在MVC 4中实现角色时出错

时间:2012-12-31 21:36:08

标签: asp.net-mvc-4 membership

我正在尝试将用户注册到某个角色,但我收到以下错误:

No user found was found that has the name "MyName"

我使用默认模板创建了一个MVC 4应用程序。

我创建了一个自定义成员资格提供程序并实现了CreateUser方法,如下所示

public User CreateUser(User i_userToCreate)
    {

        using (var _db = new Repository())
        {

           User _user = _db.CreateUser(i_userToCreate);
           MembershipUser _membershipUser = new MembershipUser(providerName: "ATWMembershipProvider",
           name: _user.UserName,
           providerUserKey: null,
           email: _user.Email,
           passwordQuestion: "",
           comment: "",
           isApproved: true,
           isLockedOut: false,
           creationDate: DateTime.UtcNow,
           lastLoginDate: DateTime.UtcNow,
           lastActivityDate: DateTime.UtcNow,
           lastPasswordChangedDate: DateTime.UtcNow,
           lastLockoutDate: DateTime.UtcNow);

           return _user;
        }

这就是我配置web.config的方式

<appSettings>
<add key="enableSimpleMembership" value="false"/>
<add key="autoFormsAuthentication" value="false"/>
</appSettings>
<system.web>
<membership defaultProvider="ATWMembershipProvider">
  <providers>
    <clear/>
    <add name="ATWMembershipProvider" type="AroundTheWorld.Infrastructure.ATWMembershipProvider"
         enablePasswordRetrieval="false"
         ConnetionStringName="Context"
         enablePasswordReset="true"
         requiresQuestionAndAnswer="false"
         equiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5"
         minRequiredPasswordLength="6"
         minRequiredNonalphanumericCharacters="0"
         passwordAttemptWindow="10" applicationName="myApplication" />
  </providers>
</membership>
<roleManager enabled="true" defaultProvider="AspNetSqlRoleProvider">
  <providers>
    <remove name="AspNetSqlRoleProvider" />
    <add name="AspNetSqlRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <!-- note: WebMatrix registers SimpleRoleProvider with name
     'AspNetSqlRoleProvider'. I don't know why but i kept it. -->
  </providers>
</roleManager>

现在验证方法如下:

 var MembershipProvider = new ATWMembershipProvider();
            User authentictedUser = MembershipProvider.CreateUser(_userToCreate);

            FormsAuthentication.SetAuthCookie(authentictedUser.UserName, true);
            System.Web.Security.Roles.AddUserToRole(authentictedUser.UserName, authentictedUser.Role.Name);
            Session.Add("UserID", authentictedUser.ID);
            Session.Add("UserName", authentictedUser.UserName);

我已经检查了以下内容:

  1. 我的用户作为所有必要信息
  2. 我在App_start“user”,“admin”
  3. 中注册了两个角色

    但我仍然收到一个错误,找不到用户找到的名为“MyName”的错误

    在asp网页表单中,为了保护基于角色的文件夹不是必需的

    我所要做的就是确保控制器内部的动作能够保护特定的“角色”

    [Authorize(Roles = "admin")]
        public ActionResult GetAllLocations()
        {
            using (var _db = new Repository())
            {
                return View(_db.GetLocations());
            }
        }
    

1 个答案:

答案 0 :(得分:2)

我已经解决了这个问题......

问题是因为我只实现了没有角色提供程序的自定义成员资格提供程序。

我已关注此blog

基本上我创建了一个继承自MyOwnRoleProvider的班级RoleProvider 并且:

  1. 实施了名为GetRolesForUser
  2. 的方法
  3. 实施了名为GetUsersInRole
  4. 的方法

    最后我将web.config修改为

    <roleManager defaultProvider="ATWRoleProvider" enabled="true" cacheRolesInCookie="true">
      <providers>
        <clear />
        <add name="ATWRoleProvider" type="AroundTheWorld.Infrastructure.ATWRoleProvider, AroundTheWorld" connectionStringName="Context" />
      </providers>
    </roleManager>
    

    之后我能够使用以下

    [Authorize(Roles="admin")]
    public ActionResult GetAllLocations()
    {
      using (var _db = new Repository())
      {
        return View(_db.GetLocations());
      }
    }
    

    @if (User.IsInRole("user"))
    

    我希望这会对某人有所帮助......