gpt4 book ai didi

c# - Asp.Net Core WebApi 的身份验证失败并显示 "Unprotect ticket failed"

转载 作者:行者123 更新时间:2023-11-30 16:38:43 26 4
gpt4 key购买 nike

当我将 Bearer token 与受 [Authorize] 保护的 AspNetCore Controller 一起使用时,我收到日志消息:

info: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler[7]
Identity.Application was not authenticated. Failure message: Unprotect ticket failed

我试图了解这意味着什么以及可能导致这种情况的原因。

Api 的 Startup 类具有以下设置。 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";
});
}

和:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseAuthentication();
app.UseMvc();
}

对调用者的响应是未授权(401),没有任何解释。

编辑:

我认为这与评论中建议的 cookie 有关。我看到一个 cookie Identity.Application。我清除了这个并尝试但没有帮助。我认为这可能与我的 token 服务器和 Api 服务器的设置方式有关(两者都使用 AspNet Identity)。

我有一个 Mvc 项目作为 Idp 在 localhost:5000 上运行。然后我的具有 protected Controller 的用户管理器 Api 托管在 localhost:5001 上。当我尝试访问 protected Controller 时,我被重定向到 IdP 项目中的登录页面(我认为这是设置 cookie 的地方)。然后我尝试将 token 与 Controller 一起使用,但出现上述错误。

如果我在获取 token 和进行 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]

最佳答案

当我在端口 5000 上启动开发服务器时,我还收到“Unprotect ticket failed”错误消息。在 Chrome 中,我有一些来自另一个项目的 cookie,该项目也以 5000 运行。删除了所有 cookie,错误消息消失了。

关于c# - Asp.Net Core WebApi 的身份验证失败并显示 "Unprotect ticket failed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54633579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com