【发布时间】: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