C# 如何在ASP.NET标识中添加声明

C# 如何在ASP.NET标识中添加声明,c#,asp.net-mvc-5,asp.net-identity,C#,Asp.net Mvc 5,Asp.net Identity,我试图找到一个文档或示例,说明如何使用ASP.NET identity在MVC 5中向用户标识添加自定义声明。该示例应显示在OWIN安全管道中插入声明的位置,以及如何使用表单身份验证将声明持久化到cookie中 也许这有助于: var claims = new List<Claim>(); claims.Add(new Claim(ClaimTypes.Name, "Brock")); claims.Add(new Claim(ClaimTypes.Email, "brockalle

我试图找到一个文档或示例,说明如何使用ASP.NET identity在MVC 5中向用户标识添加自定义声明。该示例应显示在OWIN安全管道中插入声明的位置,以及如何使用表单身份验证将声明持久化到cookie中

也许这有助于:

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);
var索赔=新列表();
添加(新索赔(ClaimTypes.Name,“Brock”);
索赔。添加(新索赔(ClaimTypes.Email,)brockallen@gmail.com"));
var id=新的索赔实体(索赔、DefaultAuthenticationTypes.ApplicationOkie);
var ctx=Request.GetOwinContext();
var authenticationManager=ctx.Authentication;
authenticationManager.SignIn(id);

如果您使用的是ASP.NET MVC 5项目模板,则添加索赔的正确位置在
ApplicationUser.cs
中。只需在此处搜索
添加自定义用户声明
。这将引导您使用
GenerateUserIdentityAsync
方法。这是当ASP.NET标识系统检索到ApplicationUser对象并需要将其转换为ClaimsEntity时调用的方法。您将看到这行代码:

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

// Add custom user claims here
最后,它返回标识:

return userIdentity;
因此,如果要添加自定义声明,则
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;

如果要在注册时添加自定义索赔,则此代码将起作用:

            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

您可以在WebAPI C中执行以下操作#


..

假设我添加了一个声明,如so
newclaim(“FName”、“John”)
那么我如何在Razor视图中访问该属性?
var userWithClaims=(ClaimsPrincipal)User
var fname=userWithClaims.Claims.First(c=>c.Type==“fname”)
可能希望将此类代码放入控制器(或者所有控制器的基类),然后将值放入模型或ViewBag中。但从技术上讲,它将直接在Razor视图中工作,正如我在这里所写的。在VS2015中,您将在
App_Start
目录下的
IdentityModels.cs
文件中找到
GenerateUserIdentityAsync()
方法。@MatthewT.Baker:我想您的意思是IdentityModels.cs在Models目录中,而不是App_Start?另外,我认为这个解决方案是不完整的。Microsoft关于添加自定义声明的文档(我认为到目前为止的评论)没有解释使用userIdentity.AddClaim(将声明添加到Cookie中,但似乎没有更改AspNetUserClaims表)和manager.AddClaim(userId,claim)的不同效果,后者将声明添加到AspNetUserClaims表中(注意,即使存在具有相同值的现有记录,它也会添加一个新记录),但不会添加cookies。Brock Allen…尼斯。尼斯一个传奇你是我的英雄:)你能展示一个完整的示例,其中包含验证密码的登录代码,还可以更新2020…注销如何?我尝试了此操作,但无法注销或删除cookie。有什么想法吗?我理解这一点,但它在默认情况下会做什么,以及需要设置什么,这让人感到困惑。电子邮件。。。。我没有作为对ClaimsPrincipal(用户)的索赔,我得到上面可能会添加它。。。但是另一个是关于什么的,因为他们没有给出调用代码的内容,。如果你知道,你能帮助我吗。例如_signInManager.PasswordSignInAsync(model.UserName,model.Password,model.RememberMe,lockoutOnFailure:false)发生了什么事;
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));