![](/img/trans.png)
[英]Obtain IsPersistent setting from IdentityServer4 openIdConnect
[英]IdentityServer OpenIdConnect adding an api as a scope
我有一個在 localhost:44387 上運行的項目,它是 IdentityServer 配置。 我有一個在 localhost:44373 上運行的 ASP.NET Core 應用程序,它充當用戶參與的前端應用程序,另一個在 localhost:44353 上運行的 ASP.NET Core 應用程序充當 API。
當用戶嘗試訪問前端應用程序中的授權控制器時,他們將被重定向到 IdentityServer 上的登錄頁面。 一旦用戶登錄,他們就會被重定向回來。
然后它們在前端應用程序上獲得授權,但是當調用 localhost:44353 上的 API 時,它返回未經授權。
我試圖向 .OpenIdConnect 方法添加一個范圍以將 API 添加為一個范圍,但它在重定向到登錄頁面時使應用程序崩潰。
如何將 API 添加為請求權限,以便前端應用程序獲得授權后可以調用 API?
這是在 IdentityServer 的 Config.cs 文件中
new Client
{
ClientId = "mvc",
ClientName = "MVC Client",
AllowedGrantTypes = GrantTypes.Implicit,
// where to redirect to after login
RedirectUris = { "https://localhost:44373/signin-oidc" },
// where to redirect to after logout
PostLogoutRedirectUris = { "https://localhost:44373/signout-callback-oidc" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"roles",
"staff_api" // <---- Add staff api as scope
},
RequireConsent = false,
}
在前端應用程序的啟動內部
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "oidc";
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect("oidc", options =>
{
options.Authority = baseAuthAddress;
options.RequireHttpsMetadata = false;
options.ClientId = "mvc";
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
//options.Scope.Add("staff_api"); <--- THIS MAKES IT CRASH?
options.Scope.Add("roles");
// Fix for getting roles claims correctly :
options.ClaimActions.MapJsonKey("role", "role", "role");
options.TokenValidationParameters.NameClaimType = "name";
options.TokenValidationParameters.RoleClaimType = "roles";
});
API 內部 Startup.cs
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
options.Audience = "staff_api"; ;
options.Authority = Configuration["AuthURL"];
});
您是否在 IdentityServer 端添加並播種了ApiResource
和ApiScope
? (使用較新版本的 IdentityServer)
就像快速入門中顯示的那樣? 因為我們沒有看到完整的 Config.cs 文件,所以首先要檢查。 您還應該查看 IS4 的 .well .well-known/openid-configuration
,以查看 api 的范圍是否在scopes_supported
部分中scopes_supported
(另請參閱快速入門鏈接)。
IdentityServer 的 Debug 輸出、API 端的TokenValidationMiddleware
和客戶端的AuthenticationMiddleware
非常冗長,您應該檢查調試輸出中是否有條目通知您什么不起作用。
此外,如果不是 SPA,則不GrantTypes.Implicit
用於 Asp.Net Core 應用程序,此類型適用於基於 JS 的前端。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.