1 回答

TA贡献1827条经验 获得超9个赞
虽然这种设计不会发生任何不好的事情,但我会完全改造它。为什么?因为您正在混合 aClient和 an ApiResource,它们应该在逻辑上分开。AClient是一个应用程序,一些用户与之交互的东西,即使它是一个无头的应用程序(即自动化服务);而 anApiResource由提供给 Clients 的资源组成,因此没有用户可以直接与其交互。
您可以针对 IdentityServer 添加两种身份验证,一种作为 API(并将其添加为JwtBearer),另一种作为客户端(并将其添加为Cookies)。然后,您可以根据该动作/控制器的功能使用[Authorize(AuthenticationSchemes = "JwtBearer")]和。= "Cookies"
撇开这一点不谈,问题是您的应用程序正在为 MVC 端获取一个身份,而为 API 端获取一个身份,因为它无法告诉您想要哪个身份。
只要你有一个想法,这就是我的一个带有 ASP.NET Core Identtiy 的 IdentityServer 的样子,你可以使用 UI 登录它,还可以使用 JwtToken 访问 REST 端点:
services
.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
})
.AddIdentityServerAuthentication(JwtBearerDefaults.AuthenticationScheme, options =>
{
options.Authority = Configuration["IdentityServerUrl"];
options.ApiName = Configuration["ApiName"];
options.RequireHttpsMetadata = false;
})
.AddCookie(IdentityConstants.ApplicationScheme, o =>
{
o.LoginPath = new PathString("/Account/Login");
o.Events = new CookieAuthenticationEvents()
{
OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync
};
})
.AddCookie(IdentityConstants.ExternalScheme, o =>
{
o.Cookie.Name = IdentityConstants.ExternalScheme;
o.ExpireTimeSpan = TimeSpan.FromMinutes(5.0);
})
.AddCookie(IdentityConstants.TwoFactorRememberMeScheme, o =>
{
o.Cookie.Name = IdentityConstants.TwoFactorRememberMeScheme;
})
.AddCookie(IdentityConstants.TwoFactorUserIdScheme, o =>
{
o.Cookie.Name = IdentityConstants.TwoFactorUserIdScheme;
o.ExpireTimeSpan = TimeSpan.FromMinutes(5.0);
});
- 1 回答
- 0 关注
- 136 浏览
添加回答
举报