【问题标题】:unauthorize access with authorization header signalr使用授权标头信号器进行未经授权的访问
【发布时间】:2016-09-05 14:55:23
【问题描述】:

我正在使用 signalR,并且正在尝试使 [Authorize] 属性起作用。通过以下设置,我得到 401 Unauthorized..

就在我启动 Hub 之前,我这样设置授权:

$.signalR.ajaxDefaults.headers = { Authorization: 'Bearer ' + settingsService.getItem('authData').token }

我可以看到标头是在请求中发送的,如下所示:

授权:承载F0wGNa7cAwUjOFI27TDR_w7N4Ncmz66PGpsU1AH2AWt0Gdt39e2o4DGwPsBXTAlIwHrAF-YHE9I1KGLxfabE0QxpcY5mLn1gxGWStOSX_W5NaUQlRlpRu5k-s6YLH-vVjlakqap_YXbzPelZJOjwcz7Ea5VHcCUFQ5xDYYK0VJXDIqMwQXZPIyVtNVu1RyLLVj7iOZaMd-41gHKWNqFWJBmK5WkWw06dI4AWiifWJT_8v1WrFPCAzYfiT0U P>

我的中心:

[Authorize]
[HubName("myHub")]
public class DataHub : Hub {
    private static bool _isInitated;

    public DataHub() {
         //Do stuff
        }
    }
}

令牌是这样创建的:

var identity = new ClaimsIdentity(OAuthDefaults.AuthenticationType);
        identity.AddClaim(new Claim("Username", "myname"));

        var properties = new AuthenticationProperties() {
            IssuedUtc = DateTime.UtcNow,
            ExpiresUtc = DateTime.UtcNow.Add(Startup.OAuthOptions.AccessTokenExpireTimeSpan)
        };

        var ticket = new AuthenticationTicket(identity, properties);
        var accessToken = Startup.OAuthOptions.AccessTokenFormat.Protect(ticket);

        var response = new JObject(new JProperty("access_token", accessToken), new JProperty("expires_in", Startup.OAuthOptions.AccessTokenExpireTimeSpan.TotalMinutes));

        return Ok(new { token = response });

我在这里错过了什么特别的东西吗?真的不明白。

【问题讨论】:

    标签: c# authorization signalr


    【解决方案1】:

    如果这对其他人来说是个问题,我在这里得到了答案。在我的 startup.cs 中,我必须在 Configuration 方法中最后移动 app.MapSignalR();。如下:

    public class Startup {
        public void Configuration(IAppBuilder app) {
            CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
            GlobalHost.Configuration.DefaultMessageBufferSize = 100;
            ConfigureOAuth(app);
            app.MapSignalR();
        }
    
        public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
    
        public void ConfigureOAuth(IAppBuilder app) {
            OAuthOptions = new OAuthAuthorizationServerOptions {
                AllowInsecureHttp = true,
                TokenEndpointPath = new PathString("/token"),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(7)
            };
    
            app.UseOAuthAuthorizationServer(OAuthOptions);
            app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
        }
    }
    

    在我有ConfigureOAuth(app); 之前.. 然后它没有工作

    【讨论】:

    • 谢谢!我搜索了几个小时试图解决这个问题。这正是我所需要的。
    猜你喜欢
    • 2012-04-04
    • 2014-05-09
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2020-05-27
    • 1970-01-01
    • 2016-08-12
    相关资源
    最近更新 更多