我正在学习IdentityServer和asp.net核心,我试图将Asp.net Identity用作用户。
基本设置是具有Authorize
属性和IdentityServer4的SignalR Hub。
如果我使用AddTestUsers()
,我可以使用ResourceOwnerPassword
创建一个accessstoken,然后连接到SignalR Hub而不会出现问题。
如果我添加Asp.net身份,我仍然可以使用ResourceOwnerPassword
获得合法的accessstokens,但是在信号调用呼叫中以某种方式不使用accesstoken。相反,我在serverlogs中看到,它使用CookieAuthenticationHandler
并尝试重定向到/ account / login。我在这里错过了什么,或者我错误配置了管道?
SERVERLOG:
info:Microsoft.AspNetCore.Hosting.Internal.WebHost [1] 请求启动HTTP / 1.1 GET http://localhost:53802/TestHub
信息: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService [2] 授权失败。
信息: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler [12] AuthenticationScheme:Identity.Application受到质疑。
info:Microsoft.AspNetCore.Hosting.Internal.WebHost [2] 要求在63.6591ms 302
完成info:Microsoft.AspNetCore.Hosting.Internal.WebHost [1] 请求启动HTTP / 1.1 GET http://localhost:53802/account/login?returnUrl=%2FTestHub
info:Microsoft.AspNetCore.Hosting.Internal.WebHost [2] 要求在3.5257ms 404完成
ConfigureServices:
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR();
services.AddIdentity<AppUser, ElCamino.AspNetCore.Identity.AzureTable.Model.IdentityRole>((options) =>
{
options.User.RequireUniqueEmail = true;
}).AddAzureTableStoresV2<ApplicationDbContext>(() =>
{
IdentityConfiguration idconfig = new IdentityConfiguration
{
TablePrefix = "v2",
StorageConnectionString = connectionString,
LocationMode = "PrimaryOnly"
};
return idconfig;
})
.AddDefaultTokenProviders();
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddAspNetIdentity<AppUser>();
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:53802";
options.RequireHttpsMetadata = false;
options.ApiName = "api1";
});
}
这是配置:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime applicationLifetime)
{
app.UseIdentityServer();
app.UseAuthentication();
app.UseSignalR(routes =>
{
routes.MapHub<TestHub>("/TestHub");
});
}