【问题标题】:SignalR 403 status code on websocket transport handshakeWebsocket 传输握手上的 SignalR 403 状态代码
【发布时间】:2019-12-29 05:53:36
【问题描述】:

我正在通过 Startup.cs 类中的代码在 Web Api 中映射 signalR

public void Configuration(IAppBuilder app)
    {

        ConfigureAuth(app);
        app.Map("/signalr", map =>
        {
            map.UseCors(CorsOptions.AllowAll);
            var hubConfiguration = new HubConfiguration
            {
                EnableDetailedErrors= true,
                EnableJSONP=true
            };
            map.RunSignalR(hubConfiguration);
        });
    }

随之,我通过以下代码在 Web Api 中使用 Bearer 令牌身份验证和 cookie 身份验证

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            AuthenticationMode = AuthenticationMode.Active,
            CookieHttpOnly = true,
            CookieSecure = CookieSecureOption.SameAsRequest,
            CookiePath = "/",
            CookieDomain = "xxxx.cloudapp.net",
        });
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        PublicClientId = "self";
        OAuthOptions = new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/Token"),
            Provider = new ApplicationOAuthProvider(PublicClientId),
            AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
            AllowInsecureHttp = true,
        };

        app.UseOAuthBearerTokens(OAuthOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
        {
            Provider = new QueryStringOAuthBearerProvider("Token")
        });

Web Api 位于不同的域中,因此我通过以下代码为 Api 调用启用了 Cors

public static void Register(HttpConfiguration config)
    {
        var cors = new    EnableCorsAttribute("http://localhost:8080,http://www.myweb.com,http://myweb.com", "*", "*");
        cors.SupportsCredentials = true;
        config.EnableCors(cors);   
     ......
     ......
     }

现在,从客户端连接时,我在控制台中收到以下错误

这是我收到的与 websocket 握手相对应的响应

请指导一下。

【问题讨论】:

  • 确保您在 Web Api 应用程序(服务器)中启用/支持 websocket。
  • 在云服务中启用
  • 不确定这是否与此有关,但如果您使用的是框架 4.5,您是否在 web.config > appSettings 标记中设置了

标签: websocket signalr owin signalr-hub


【解决方案1】:

我知道这是一个老问题,但我也遇到了同样的问题,所以如果其他人偶然发现它以供将来参考......

不知何故:

CookieSecure = CookieSecureOption.SameAsRequest,

是问题所在。 它不能正常工作(不知道为什么)......

这是我的 cookie 配置:

            builder.Register(ctx => new CookieAuthenticationOptions
            {
                AuthenticationMode = AuthenticationMode.Active,
                AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
                CookieName = "xxx",
                CookieHttpOnly = false, // Kako bi mu mogli pristupiti iz Javascripta
                ExpireTimeSpan = TimeSpan.FromDays(1),
                LoginPath = PathString.Empty,
                LogoutPath = PathString.Empty,
                SlidingExpiration = true,
#if DEBUG
                CookieSecure = CookieSecureOption.SameAsRequest,
#else
                CookieSecure = CookieSecureOption.Always,
#endif
                CookieDomain = "localhost"
            });

这是我的令牌配置

            builder.Register(ctx => new OAuthAuthorizationServerOptions
            {
                AuthorizeEndpointPath = new PathString("/api/authorize"),
                TokenEndpointPath = new PathString("/api/token"),
                ApplicationCanDisplayErrors = true,
                Provider = ctx.Resolve<ApplicationOAuthProvider>(),
                //RefreshTokenProvider = ctx.Resolve<ApplicationRefreshTokenProvider>(),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
#if DEBUG
                AllowInsecureHttp = true
#endif
            });

在 HTTPS 本地主机上托管我的应用程序后,它自动运行。 为什么?还是不知道:P

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 2016-11-05
    • 1970-01-01
    • 2018-06-04
    • 2019-05-18
    • 1970-01-01
    • 2012-07-09
    相关资源
    最近更新 更多