swd*_*don 6 c# authentication oauth-2.0 asp.net-core-identity asp.net-core-2.2
当我将 Bearer 令牌与受保护的 AspNetCore 控制器一起使用时[Authorize],我收到日志消息:
info: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler[7]
Identity.Application was not authenticated. Failure message: Unprotect ticket failed
Run Code Online (Sandbox Code Playgroud)
我试图了解这意味着什么以及可能导致这种情况的原因。
StartupApi的类具有以下设置。Api 使用 AspNet Identity Core。
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<UserAccountDbContext>(options => options.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(MigrationsAssembly)));
services.AddIdentity<UserAccount, IdentityRole>()
.AddEntityFrameworkStores<UserAccountDbContext>();
services.AddTransient<UserManager<UserAccount>>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddAuthorization();
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
options.Authority = _configuration.OAuth2.ServerUri;
options.RequireHttpsMetadata = false;
options.Audience = "api";
});
}
Run Code Online (Sandbox Code Playgroud)
和:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
对调用者的响应是 Unauthorized (401),没有任何解释。
编辑:
正如评论所建议的那样,我认为这与 cookie 有关。我看到一个饼干Identity.Application。我清除了这个并尝试但没有帮助。我认为这可能与我的令牌服务器和 Api 服务器的设置方式有关(两者都使用 AspNet Identity)。
我有一个 Mvc 项目作为 localhost:5000 上的 Idp 运行。然后我的具有受保护控制器的用户管理器 Api 托管在 localhost:5001 上。当我尝试访问受保护的控制器时,我被重定向到 IdP 项目中的登录页面(我认为这是设置 cookie 的原因)。然后我尝试将令牌与控制器一起使用,但出现上述错误。
如果我在获取令牌和进行 Api 调用之间删除 cookie,我会得到以下日志:
2019-02-11 23:35:15.3711 [INFO] Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
info: Microsoft.AspNetCore.Mvc.ChallengeResult[1]
Executing ChallengeResult with authentication schemes ().
2019-02-11 23:35:15.3711 [INFO] Executing ChallengeResult with authentication schemes ().
info: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler[12]
AuthenticationScheme: Identity.Application was challenged.
2019-02-11 23:35:15.3711 [INFO] AuthenticationScheme: Identity.Application was challenged.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Run Code Online (Sandbox Code Playgroud)
当我在端口 5000 上启动开发服务器时,我还收到“取消保护票证失败”错误消息。在 Chrome 中,我有很多来自另一个项目的 cookie,该项目也以 5000 的速度运行。删除了所有 cookie,错误消息消失了。
| 归档时间: |
|
| 查看次数: |
10860 次 |
| 最近记录: |