Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 授权在ASP.NET Core 2.1的信号器中不起作用_Asp.net Core_Asp.net Core 2.0_Asp.net Core Webapi_Asp.net Core Signalr_Asp.net Core 2.1 - Fatal编程技术网

Asp.net core 授权在ASP.NET Core 2.1的信号器中不起作用

Asp.net core 授权在ASP.NET Core 2.1的信号器中不起作用,asp.net-core,asp.net-core-2.0,asp.net-core-webapi,asp.net-core-signalr,asp.net-core-2.1,Asp.net Core,Asp.net Core 2.0,Asp.net Core Webapi,Asp.net Core Signalr,Asp.net Core 2.1,我已经通过以下方式将我的项目从ASP.NETCore2.0升级到ASP.NETCore2.1 在我将Signar Core 2.1应用于我的项目之前,一切都很好 这是我的Startup.cs 这是我的ChatHub.cs: 我所期望的是,当我使用AngularJS应用程序连接到ChatHub时,会调用MainPolicy。但是,调用OnConnectedAsync函数时未检查请求标识 MVC控制器的策略已成功应用,但Signalr的策略未成功应用 有人能帮我吗 谢谢,我将此问题发布到Signal

我已经通过以下方式将我的项目从ASP.NETCore2.0升级到ASP.NETCore2.1

在我将Signar Core 2.1应用于我的项目之前,一切都很好

这是我的Startup.cs

这是我的ChatHub.cs:

我所期望的是,当我使用AngularJS应用程序连接到ChatHub时,会调用MainPolicy。但是,调用OnConnectedAsync函数时未检查请求标识

MVC控制器的策略已成功应用,但Signalr的策略未成功应用

有人能帮我吗


谢谢,

我将此问题发布到Signalr github上。 这是他们给我的答案。 我试过了,结果成功了:

解决方案是将[Authorize]属性放在ChatHub上


只需与不知道的人分享:

我在Signalr github上发布了这个问题。 这是他们给我的答案。 我试过了,结果成功了:

解决方案是将[Authorize]属性放在ChatHub上


只需告诉不知道的人:

我也有同样的问题,有四个关键点:

1-在Startup.cs中,请注意ConfigureIApplicationBuilder应用程序中的此顺序

3-当您的客户机想要建立连接时,应该发送令牌。您可以在建立连接时向查询添加令牌

var connection = new signalR.HubConnectionBuilder().withUrl(
"http://localhost:5000/chat", {
    skipNegotiation: true,
    transport: signalR.HttpTransportType.WebSockets,
    accessTokenFactory: () => "My Token Is Here"}).build();
4-我没有在services.AddAuthentication中添加默认的访问方案 所以每次我都必须像这样指定我的授权方案。[AuthorizationAuthenticationSchemes=JwtBearerDefaults.AuthenticationScheme] 最后,您可以像这样保护您的聊天类

using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication.JwtBearer;

        [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
        public class myChat : Hub
        {
            ///Some functions
        }
似乎使用语句很重要,所以请确保使用正确的语句。


注意:我在myChat类中只授权一个方法时遇到问题。我不知道为什么。

我也有同样的问题,有四个关键点:

1-在Startup.cs中,请注意ConfigureIApplicationBuilder应用程序中的此顺序

3-当您的客户机想要建立连接时,应该发送令牌。您可以在建立连接时向查询添加令牌

var connection = new signalR.HubConnectionBuilder().withUrl(
"http://localhost:5000/chat", {
    skipNegotiation: true,
    transport: signalR.HttpTransportType.WebSockets,
    accessTokenFactory: () => "My Token Is Here"}).build();
4-我没有在services.AddAuthentication中添加默认的访问方案 所以每次我都必须像这样指定我的授权方案。[AuthorizationAuthenticationSchemes=JwtBearerDefaults.AuthenticationScheme] 最后,您可以像这样保护您的聊天类

using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication.JwtBearer;

        [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
        public class myChat : Hub
        {
            ///Some functions
        }
似乎使用语句很重要,所以请确保使用正确的语句。


注意:我在myChat类中只授权一个方法时遇到问题。我不知道为什么。

授权属性的名称空间是什么?授权属性的名称空间是什么?
            app.UseRouting();
            app.UseAuthorization( );
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<myChat>("/chat");
            });
  services.AddAuthentication()
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata = false;
                options.SaveToken = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateAudience = false,
                    ValidIssuer = [Issuer Site],
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes([YOUR SECRET KEY STRING]))
                };
                options.Events = new JwtBearerEvents
                {
                    OnMessageReceived = context =>
                    {
                        var path = context.Request.Path;
                        var accessToken = context.Request.Query["access_token"];
                        if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments("/chat"))
                        {
                            
                            context.Request.Headers.Add("Authorization", new[] { $"Bearer {accessToken}" });
                        }
                        return Task.CompletedTask;
                    }
                };
            });
var connection = new signalR.HubConnectionBuilder().withUrl(
"http://localhost:5000/chat", {
    skipNegotiation: true,
    transport: signalR.HttpTransportType.WebSockets,
    accessTokenFactory: () => "My Token Is Here"}).build();
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication.JwtBearer;

        [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
        public class myChat : Hub
        {
            ///Some functions
        }