从ASP.NET Identity 2.0.1中的角色中删除用户 [英] Remove User from Roles in ASP.NET Identity 2.0.1

查看:67
本文介绍了从ASP.NET Identity 2.0.1中的角色中删除用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定制了身份系统的一些功能以满足我的需求。



我有以下ApplicationUserManager:



I customized some functionalities of Identity system to fit my needs.

I have the following ApplicationUserManager:

public class ApplicationUserManager : UserManager<ApplicationUser, long>
{
    //Constructors and other methods...

    public bool AddUserToRole(long userId, string roleName)
    {
        return this.AddToRole(userId, roleName).Succeeded;
    }

    public bool ClearAllUserRoles(long userId)
    {
        foreach (var role in this.GetRoles(userId))
        {
            if (!this.RemoveFromRole(userId, role).Succeeded)
            {
                return false;
            }
        }
        return true;
    }
}





其他自定义类是:





And the other custom classes are:

public class ApplicationUser : IdentityUser<long, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public virtual UserInfo UserInfo { get; set; }

}

public class ApplicationRole : IdentityRole<long, ApplicationUserRole>
{
    public ApplicationRole() : base() { }
    public ApplicationRole(strin//sets...tring description)
    {
        //sets...
    }
    public virtual string Description { get; set; }
}

public class ApplicationUserRole : IdentityUserRole<long> { }





添加用户并将其添加到角色似乎工作正常。我的问题是当我想从角色中删除用户时。



当我想将用户添加到角色列表时,我开始从所有角色中清除用户他已经使用了方法`ClearAllUserRoles(long userId)`并将我添加到每个选定的角色后。



我的问题是`RemoveFromRole(userId,在`ClearAllUserRoles(long userId)'中使用的`方法'只将我的`AspNetUserRoles`表的字段`ApplicationUser_Id`和`ApplicationRole_Id`置为null,使得'UserId`和`RoleId`字段填满,所以当我尝试重新分配时抛出了他已经处于以下异常中的角色的用户:



{违反PRIMARY KEY约束'PK_dbo.AspNetUserRoles'。无法插入重复键对象'dbo.AspNetUserRoles'。重复的键值是(16,1)。\\\\ n语句已被终止。}



这是我的`OnModelCreating()`方法:





Adding users and adding them to roles seems to be working fine. My problem is when I want to remove a user from roles.

When I want to Add user to a list of roles, I start clearing the user from all roles that he is already in with the method `ClearAllUserRoles(long userId)` and after I add him to each selected role.

My problem is that the `RemoveFromRole(userId, role)` method used in `ClearAllUserRoles(long userId)` only puts the fields `ApplicationUser_Id` and `ApplicationRole_Id` of my `AspNetUserRoles` table to null, leaving the fields `UserId` and `RoleId` filled so when I try to reassign the user to a role that he was already in the following exception is thrown:

{"Violation of PRIMARY KEY constraint 'PK_dbo.AspNetUserRoles'. Cannot insert duplicate key in object 'dbo.AspNetUserRoles'. The duplicate key value is (16, 1).\r\nThe statement has been terminated."}

This is my `OnModelCreating()`method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        EntityTypeConfiguration<ApplicationUser> table =
            modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers");

        modelBuilder.Entity<ApplicationUser>().HasMany<ApplicationUserRole>((ApplicationUser u) => u.Roles);
        modelBuilder.Entity<ApplicationUserRole>().HasKey((ApplicationUserRole r) =>
            new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("AspNetUserRoles");

        EntityTypeConfiguration<ApplicationRole> entityTypeConfiguration1 =
            modelBuilder.Entity<ApplicationRole>().ToTable("AspNetRoles");

        entityTypeConfiguration1.Property((ApplicationRole r) => r.Name).IsRequired();

    }





在我的迁移中,create table是这样的:





And in my Migration, the create table is like this:

CreateTable(
            "dbo.AspNetUserRoles",
            c => new
                {
                    UserId = c.Long(nullable: false),
                    RoleId = c.Long(nullable: false),
                    ApplicationUser_Id = c.Long(),
                    ApplicationRole_Id = c.Long(),
                })
            .PrimaryKey(t => new { t.UserId, t.RoleId })
            .ForeignKey("dbo.AspNetUsers", t => t.ApplicationUser_Id)
            .ForeignKey("dbo.AspNetRoles", t => t.ApplicationRole_Id)
            .Index(t => t.ApplicationUser_Id)
            .Ind;x(t => t.ApplicationRole_Id);





我的问题是:



1.应该我的`AspNetUserRoles`表有4列(`ApplicationUser_Id`,`ApplicationRole_Id`,`UserId`,`RoleId`)或者我在数据库上下文的`OnModelCreating()`方法中做错了什么?



2.`RemoveFromRole(userId,role)`方法是否按预期工作,或者它应该删除`AspNetUserRoles`表的所有行?



My Questions are:

1. Should my `AspNetUserRoles` table have those 4 columns (`ApplicationUser_Id`, `ApplicationRole_Id`, `UserId`,`RoleId`) or I am doing something wrong in the `OnModelCreating()`method of my database context?

2. Is the `RemoveFromRole(userId, role)` method working as expected or it should delete the all row of `AspNetUserRoles` table?

推荐答案

我不确定为什么你对ASP.NET引擎中的默认角色提供程序不满意(特别是因为你是初学者),但是如果你想控制角色处理的每个方面你最好学习如何创建自己的角色提供者...

从这里开始: http://msdn.microsoft.com/en-us/library/vstudio/8fw7xh74(v = vs.100).aspx [ ^ ]
I'm not sure why you are not satisfied with default role provider from the ASP.NET engine (specially as you are a beginner), however if you want to control every aspect of role handling you better learn how to create your own role provider...
Start here: http://msdn.microsoft.com/en-us/library/vstudio/8fw7xh74(v=vs.100).aspx[^]


这篇关于从ASP.NET Identity 2.0.1中的角色中删除用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆