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 關注
- 155 瀏覽
添加回答
舉報