gpt4 book ai didi

c# - 转换/修改 asp.net identity 2 中的声明

转载 作者:行者123 更新时间:2023-11-30 14:53:20 24 4
gpt4 key购买 nike

在 Windows Identity Framework (WIF) 中,您可以实现 ClaimsAuthenticationManager 以修改主体的声明或向其添加新的声明。

The claims authentication manager provides an extensibility point in the application’s claims processing pipeline that you can use to validate, filter, modify, incoming claims or inject new claims into the set of claims presented by a ClaimsPrincipal before the RP application code is executed.

ASP.net Identity 2 有没有像这样的管道 Hook ?如果我想添加一些声明而不让它们保留在 AspNetUserClaims 表中,我该怎么做?

最佳答案

执行此操作的合乎逻辑的位置是在用户成功登录之后。这将发生在 AccountController 登录操作中:

    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid) { return View(model); }

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:

// Transform here
var freshClaims = new List<Claim>
{
new Claim(ClaimTypes.Email, model.Email),
new Claim(ClaimTypes.Locality, "Earth (Milky Way)"),
new Claim(ClaimTypes.Role, "Trooper"),
new Claim(ClaimTypes.SerialNumber, "555666777")
};
AuthenticationManager.AuthenticationResponseGrant.Identity.AddClaims(freshClaims);
return RedirectToLocal(returnUrl);

我使用 DI 将 AuthenticationManager 注入(inject)到 AccountControllers 构造函数中,并将其设置为 AccountController 的属性。如果您不这样做,那么您可以从 OWIN 上下文中获取它:

var authManager = HttpContext.Current.GetOwinContext().Authentication;
authManager.AuthenticationResponseGrant.Identity.AddClaims(freshClaims);

关于c# - 转换/修改 asp.net identity 2 中的声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29749483/

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