Asp.Net身份自定义用户角色(查询) [英] Asp.Net Identity Custom User Roles (querying)

查看:150
本文介绍了Asp.Net身份自定义用户角色(查询)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地创建自定义的用户和角色的身份Asp.Net框架内,但其中之一是缺乏部分是用户中的角色集合的支持。我加了的UserRole内的一个逻辑'删除'标志,我想这个时候EF从数据库中获取的信息被收录,但它是所有检索所有内容(这是不是我想要的)。塞纳里奥:

I have successfully created custom users and roles within the Asp.Net Identity framework, however one section which is lacking is support for the 'Roles' collection within a User. I added a logical 'Deleted' flag within a UserRole and I want this to be included when EF retrieves the info from the database, however all it does is retrieve everything (which isn't what I want). Senario:


  1. 用户A的管理员(非删除)
  2. 的角色
  3. 我再加入超级管理员的角色,然后删除管理员

  4. 如果我再访问我的版本IdentityUser的(具有角色的ICollection - 定制型)我仍然看到两个角色(1删除,没有1)

我希望发生的是登录(主要使用默认的职能,覆盖各个部分),当EF检索数据就应该知道,以排除为删除其标记任何行,但我一直无法找到任何方法这样做。因为所有的样板code被锁定,我不能很容易地看到发生了什么,或发现任何逻辑的地方覆盖此功能。

What I want to happen is on login (using mostly default functions with various sections overridden), when EF retrieves the data it should know to exclude any row which is flagged as deleted, however I have been unable to find any method of doing this. Because all the boilerplate code is locked, I cannot easily see what is happening or find any logical place to override this functionality.

任何人都可以在这方面帮助?

Can anyone help with this?

推荐答案

您可以只扫描角色登录后,用code类似这样的删除:

You could just scan the roles after login and remove them with code similar to this:

        var roles = UserManager.GetRoles(userId);
        foreach (var roleName in roles)
        {
            var role = RoleManager.FindByName(roleName);
            if (role.IsDeleted) UserManager.RemoveFromRole(userId, roleName);
        }

这篇关于Asp.Net身份自定义用户角色(查询)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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