简体   繁体   English

如何在 ASP.NET Identity 中添加声明

[英]How to add claims in ASP.NET Identity

I am trying to find a document or example of how you would add custom claims to the user identity in MVC 5 using ASP.NET Identity.我正在尝试查找有关如何使用 ASP.NET Identity 向 MVC 5 中的用户标识添加自定义声明的文档或示例。 The example should show where to insert the claims in the OWIN security pipeline and how to persist them in a cookie using forms authentication.该示例应显示在 OWIN 安全管道中插入声明的位置以及如何使用表单身份验证将它们保存在 cookie 中。

The correct place to add claims, assuming you are using the ASP.NET MVC 5 project template is in ApplicationUser.cs .添加声明的正确位置(假设您使用的是 ASP.NET MVC 5 项目模板)在ApplicationUser.cs Just search for Add custom user claims here .只需Add custom user claims here搜索Add custom user claims here This will lead you to the GenerateUserIdentityAsync method.这将引导您使用GenerateUserIdentityAsync方法。 This is the method that is called when the ASP.NET Identity system has retrieved an ApplicationUser object and needs to turn that into a ClaimsIdentity.这是在 ASP.NET Identity 系统检索到 ApplicationUser 对象并需要将其转换为 ClaimsIdentity 时调用的方法。 You will see this line of code:你会看到这行代码:

// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

After that is the comment:之后是评论:

// Add custom user claims here

And finally, it returns the identity:最后,它返回身份:

return userIdentity;

So if you wanted to add a custom claim, your GenerateUserIdentityAsync might look something like:因此,如果您想添加自定义声明,您的GenerateUserIdentityAsync可能如下所示:

// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

// Add custom user claims here
userIdentity.AddClaim(new Claim("myCustomClaim", "value of claim"));

return userIdentity;

Perhaps the following article can help:也许以下文章可以提供帮助:

var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, "Brock"));
claims.Add(new Claim(ClaimTypes.Email, "brockallen@gmail.com"));
var id = new ClaimsIdentity(claims,DefaultAuthenticationTypes.ApplicationCookie);

var ctx = Request.GetOwinContext();
var authenticationManager = ctx.Authentication;
authenticationManager.SignIn(id);

If you want to add custom claims at the time of registration then this code will work:如果您想在注册时添加自定义声明,则此代码将起作用:

            var user = new ApplicationUser
            {
                UserName = model.UserName,
                Email = model.Email
            };
            var result = await UserManager.CreateAsync(user, model.Password);
            // Associate the role with the new user 
            await UserManager.AddToRoleAsync(user.Id, model.UserRole);
            // Create customized claim 
            await UserManager.AddClaimAsync(user.Id, new Claim("newCustomClaim", "claimValue"));
            if (result.Succeeded)
            {...etc

you can do the following in WEB API C #您可以在 WEB API C# 中执行以下操作

var identity = new ClaimsIdentity(context.Options.AuthenticationType);          
        foreach(var Rol in roles)
        {
            identity.AddClaim(new Claim(ClaimTypes.Role, Rol));
        }
        identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
        identity.AddClaim(new Claim(ClaimTypes.Email, user.Correo));
        identity.AddClaim(new Claim(ClaimTypes.MobilePhone, user.Celular));
        identity.AddClaim(new Claim("FullName", user.FullName));
        identity.AddClaim(new Claim("Empresa", user.Empresa));
        identity.AddClaim(new Claim("ConnectionStringsName", user.ConnectionStringsName));

.... ....

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

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