[英]claims missing using ASP.net core identity and itfoxtec with okta
我使用此處找到的 .NET、Okta 和 ITFOXTEC 實施示例解決方案。 一切正常。 然后我嘗試將此解決方案集成到我們的主代碼庫中,該代碼庫也使用 ASP.NET 身份。 一旦我添加了身份配置,ITFoxtec 創建的任何身份似乎都被覆蓋了(登錄后沒有任何 SAML 聲明存在)。 我通過將以下內容添加到上面示例解決方案中的 Startup.cs 來重現該問題。
services.AddDbContext<ApplicationDbContext>();
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
我希望在同一解決方案中同時使用 SAML 和身份框架,但不確定它們是否兼容。
我不確定這是可能的,我從未嘗試過。 但是,我認為這是可能的。
聽起來接收到的 SAML 2.0 身份被覆蓋了,因此聲明也被覆蓋了。 如果您成功了,請分享您的發現。
我知道這有點晚了,但我們已經讓 SAML 在 ASP.NET MVC Core 6 應用程序中使用 ASP.NET 身份。 我們使用了上面提到的相同示例,並在ClaimsTransform
class 的CreateClaimsPrincipal
方法中執行此操作:
using System.Security.Claims;
using Microsoft.AspNetCore.Identity;
using ITfoxtec.Identity.Saml2.Claims;
...
// add the ASP.NET Identity roles as Claims
var idClaim = GetClaim(incomingPrincipal, Saml2ClaimTypes.NameId);
if (idClaim != null)
{
IdentityUser identityUser = await userManager.FindByEmailAsync(idClaim.Value);
if (identityUser != null)
{
foreach (var r in await userManager.GetRolesAsync(identityUser))
{
claims.Add(new Claim(type: ClaimTypes.Role, value: r));
}
}
}
我們可能應該使ClaimsTransform
非 static 並注入UserManager<IdentityUser> userManager
但現在它作為參數從AuthController
中的調用傳入。
在這個正常的 ASP.NET Identity 東西起作用之后。 授權控制器和操作的屬性: [Authorize(Roles = "Identity Role 1, Identity Role 2")]
並且User.IsInRole("Identity Role 1")
簽入代碼和.cshtml
在program.cs
中,我們最后進行 SAML 配置——就在var app = builder.Build();
之前。
您可能想要做的另一件事是阻止訪問部分/所有默認身份頁面。 我們在program.cs
中這樣做是為了防止用戶訪問注冊頁面,但您也可以搭建身份頁面並按照 Microsoft 在此處建議的那樣編輯它們。
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/Identity/Account/Register", context => Task.Factory.StartNew(() => context.Response.Redirect("/", true, true)));
endpoints.MapPost("/Identity/Account/Register", context => Task.Factory.StartNew(() => context.Response.Redirect("/", true, true)));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.