简体   繁体   English

更改/修改asp.net身份中的声明2

[英]Transforming / Modifying claims in asp.net identity 2

In Windows Identity Framework (WIF) you could implement a ClaimsAuthenticationManager in order to modify the claims on the principal or add new claims to it. 在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. 声明身份验证管理器在应用程序的声明处理管道中提供了一个可扩展点,您可以在执行RP应用程序代码之前,使用该点来验证,过滤,修改传入的声明,或将新声明插入到ClaimsPrincipal提出的声明集中。

Does ASP.net Identity 2 have any sort of pipeline hook like this? ASP.net Identity 2是否具有这样的管道挂钩? If I want to add some claims without having them persisted in the AspNetUserClaims table how can I do this? 如果我想添加一些声明而不将其保留在AspNetUserClaims表中,该怎么办?

The logical place to do this would be right after the user has successfully signed in. This would occur in the AccountController login action: 在用户成功登录后,可以立即执行此操作。这将在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);

I use DI to inject AuthenticationManager into AccountControllers constructor and set it up as a property of AccountController . 我使用DI注入AuthenticationManagerAccountControllers构造函数,并将其设置为一个属性AccountController If you don't do this then you can just get it off the OWIN context: 如果您不这样做,则可以从OWIN上下文中OWIN它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM