本文介绍了AddToRole()方法不会导致在ASP.NET身份数据库条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的ASP.NET MVC应用程序中使用ASP.NET身份。同时将用户添加到角色我的问题occures。这里没有任何异常,但作为um.AddToRole的结果()没有分贝条目添加到AspNetUserRoles表。我的操作方法看起来像:

 公众的ActionResult GrantAdmin(字符串ID)
    {
        ApplicationUser用户= um.FindById(ID);
        如果(!rm.RoleExists(管理员))
        {
            rm.Create(新IdentityRole(管理员));
        }
        um.AddToRole(user.Id,管理员);
        返回查看((对象)user.UserName);
    }

恩是的UserManager类的一个对象:

 私人的UserManager< ApplicationUser>嗯=新的UserManager&所述; ApplicationUser>(新UserStore&所述; ApplicationUser>(新ApplicationDbContext()));

可那是什么样的应用程序行为的原因是什么?任何想法?

===编辑===
这是我的DbContext:

 公共ApplicationDbContext()
        :基地(DefaultConnection)
    {
    }

和Web.config中默认连接:

 <添加名称=DefaultConnection的connectionString =数据Source=(LocalDb)\\v11.0;AttachDbFilename=|DataDirectory|\\aspnet-RecommendationPlatform-20140404055015.mdf;Initial目录= ASPNET-RecommendationPlatform-20140404055015;集成安全性=真的providerName =System.Data.SqlClient的/>


解决方案

在使用ASP.NET身份的工作是要记住,很多操作返回,其中保存最终的错误结果对象是很重要的。有没有例外。因此应该检查结果对象每次操作后成功。这不仅是对角色但该数据保存到数据库中的大多数方法如此。即使它的作品你还是应该测试成功,并最终引发异常,如果结果不是成功。

根据你的情况的意见,问题是无效的用户名。

I'm using ASP.NET Identity in my ASP.NET MVC app. My problem occures while adding user to role. There isn't any exception, but as a result of um.AddToRole() no db entry is added to AspNetUserRoles table. My action method looks like that:

    public ActionResult GrantAdmin(string id)
    {
        ApplicationUser user = um.FindById(id);
        if (!rm.RoleExists("admin")) 
        {
            rm.Create(new IdentityRole("admin"));
        }
        um.AddToRole(user.Id, "admin");
        return View((object)user.UserName);
    }

um is an object of UserManager class:

private UserManager<ApplicationUser> um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

What can be a reason of that kind of application behavior? Any idea?

===EDIT===It is my DbContext:

public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

And Default Connection in Web.config:

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-RecommendationPlatform-20140404055015.mdf;Initial Catalog=aspnet-RecommendationPlatform-20140404055015;Integrated Security=True" providerName="System.Data.SqlClient" />
解决方案

When working with ASP.NET Identity it is important to remember that many operations return a result object where eventual errors are stored. There are no exceptions. Therefore one should check the result object for success after every operation. This is true not only for roles but for most methods that save data to the database. Even if it works you should still test for success and eventually throw an exception if the result is not success.

As per the comments in your case the problem was invalid username.

这篇关于AddToRole()方法不会导致在ASP.NET身份数据库条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:48