本文介绍了ASP.NET MVC 5身份userManager.IsInRole的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码无法正常工作,我无法解释为什么...我的用户管理器是导致其创建用户和角色就好了显著窘迫,但是当我运行这段代码userManager.IsInRole总是返回假的,所以第二次我跑我的种子我击球失误,因为它试图创建,尽管它已经存在的事实的纪录!



请注意,这是当我出现我对我的迁移项目运行更新的数据库,是事实,这是一个非ASP项目造成的问题,如果是这样,为什么?应该不是一个错误被抛出。



这是我用过的身份,虽然工作时它似乎不错,很少有最新的优良品质文档可用,第一个项目,所以如果任何人有任何这个来源我将不胜感激。

 公共无效运行(BlogContext blogContext)
{
变种userStore =新UserStore<使用者>(( blogContext)blogContext);
变种的UserManager =新的UserManager<使用者>(userStore);

变种的UserRole =新的List< UserRole的>()
{
新的UserRole(){用户名[email protected]角色=系统管理员},
新的UserRole(){用户名[email protected]角色=管理员},
新的UserRole(){用户名[email protected]角色=作者}
};

的foreach(在VAR的UserRole的UserRole)
{
VAR用户id = userManager.FindByName(userRole.Username).ID;

如果(userManager.IsInRole(用户ID,userRole.Role)!)
userManager.AddToRole(用户ID,userRole.Role);
}


blogContext.SaveChanges();
}


解决方案

所以,我将在回答自己保存任何的疼痛小时,我受到影响,因为这一点。



这样做的原因发生的是,我不得不延迟加载残疾人,我已经启用了这是在我的迁移项目中,像这样。

 保护覆盖无效种子(BlogContext blogContext)
{
AutomaticMigrationsEnabled = TRUE;
blogContext.Configuration.LazyLoadingEnabled = TRUE;
//添加种子类在这里!
}


The following code does not work, and I can't explain why... My user manager is causing significant distress in that it creates users and roles just fine but when I run this code userManager.IsInRole is always returning false, so the second time I run my seed I am hitting errors because it is trying to create the record despite the fact it already exists!

Please note that this is occurring when I am running update-database against my migrations project, is the fact this is a non ASP project causing issues, if so why? shouldn't an error be thrown.

This is the first project I have used Identity and although when it works it seems good, there is very little up to date good quality documentation available, so if anyone has any sources for this I would be grateful.

    public void Run(BlogContext blogContext)
    {
        var userStore = new UserStore<User>((BlogContext) blogContext);
        var userManager = new UserManager<User>(userStore);

        var userRoles = new List<UserRole>()
        {
            new UserRole() {Username = "[email protected]", Role = "SysAdmin"},
            new UserRole() {Username = "[email protected]", Role = "Admin"},
            new UserRole() {Username = "[email protected]", Role = "Author"}
        };

        foreach (var userRole in userRoles)
        {
            var userId = userManager.FindByName(userRole.Username).Id;

            if (!userManager.IsInRole(userId, userRole.Role))
                userManager.AddToRole(userId, userRole.Role);
        }


        blogContext.SaveChanges();
    }
解决方案

So I will answer this myself to save anyone the hours of pain I suffered because of this.

The reason for this occurring was that I had lazy loading disabled, I have enabled this to be on in my Migrations project like so.

protected override void Seed(BlogContext blogContext)
    {
        AutomaticMigrationsEnabled = true;
        blogContext.Configuration.LazyLoadingEnabled = true;
        //Add seed classes here!
    }

这篇关于ASP.NET MVC 5身份userManager.IsInRole的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:10