相关疑难解决方法(0)

如何在用户注册期间添加声明

我正在使用带有身份2.1.0和VS2013 U4的ASP.NET MVC 5项目.我想在注册期间向用户添加声明,以便存储在db中.这些声明代表用户自定义属性.
当我为管理员创建一个用于创建/编辑/删除用户的网页时,我仍然使用create method AccountController来创建用户,但我不想登录该用户.如何将这些声明添加到用户?

c# asp.net-mvc asp.net-mvc-5 asp.net-identity-2

10
推荐指数
2
解决办法
2万
查看次数

使用 ASP.NET Identity 是否可以在现有会话期间动态添加和删除声明?

我尝试在 Asp.Net 4.5.2 中使用 Identity 2 成功登录后动态添加和删除声明(在本例中为角色)。我的应用程序有一个身份验证数据库,其中包含 AspNetUsers、AspNetRoles 和 AspNetUserRoles 表等以及许多其他数据库。在用户会话过程中,我的用户可以在其他数据库之间切换,并且他们当前的声明(角色)会根据他们当前使用的数据库进行修改。因此,我想在整个会话期间添加和删除声明。这允许我根据用户当前的授权修改用户有权访问的视图。

我已经在堆栈溢出和 MS Identity 帮助页面(例如它们)中对此进行了很多天的研究,但找不到与我尝试做的任何类似的内容。根据我所了解到的情况,我已经能够添加自己的新声明,但只能在登录过程中添加,在任何其他点更改它们都适用于该请求,但更改不会持续存在,并且在下一个请求时会丢失进来。

据我所知,当在登录期间添加声明时,它们会在会话 cookie 中进行编码,而当我在任何其他点添加它们时,cookie 不会被修改。根据我目前的理解,这发生在 OWIN 管道的 Identity 模块中。我成功添加声明的方法是ApplicationUser 中的GenerateUserIdentityAsync 方法(派生自IdentityUser.

    public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // 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("Customer_ID", Convert.ToString(this.Customer_ID)));
            userIdentity.AddClaim(new Claim("LastName", this.LastName));
            userIdentity.AddClaim(new Claim("FirstName", this.FirstName));

            userIdentity.AddClaim(new Claim(ClaimTypes.Role, "TestRole", null, null, "TestIssuer"));

            return userIdentity;
        }
Run Code Online (Sandbox Code Playgroud)

不幸的是,这对我的情况没有帮助,因为我正在尝试在 OWIN …

c# asp.net asp.net-mvc asp.net-identity

5
推荐指数
1
解决办法
1121
查看次数