3

I am add new roles in mvc 4 app ... but every time it gives this Error

here is my Register Method

 //
        // POST: /Account/Register
         [AllowAnonymous]
        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                object x = Membership.GetUser(model.UserName);

                if (Membership.GetUser(model.UserName) == null)
                {
                    // Attempt to register the user
                    MembershipCreateStatus createStatus;
                    Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null,
                        out createStatus);

                    if (createStatus == MembershipCreateStatus.Success)
                    {


                        Roles.AddUserToRole(model.UserName, model.Role);

 //model.Role is the DropDown Selected Text.. its value is 'Admin'

                        FormsAuthentication.SetAuthCookie(model.UserName,   
   false /* createPersistentCookie */);
                        return RedirectToAction("Index", "EUT");
                    }
                    else
                    {
                        ModelState.AddModelError("",  
     ErrorCodeToString(createStatus));
                    }

                }
                else
                {

                    ModelState.AddModelError("", "User Already exists");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

In My Web Config

 <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>

    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
      </providers>
    </profile>

    <roleManager enabled="true"  defaultProvider="AspNetSqlRoleProvider"
                                    cacheRolesInCookie="true"
                                    cookieName=".myroles"
                                    cookieTimeout="30"
                                    cookieSlidingExpiration="true"
                                    cookieProtection="All">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>

I have seen similar posts :

Add User Roles on Registration (Forms Authentication) MVC3

Adding users to roles on registring failed ASP.NET MVC3

Adding roles only adding one to user

In tried to bind selected value with model property ...still it is not working ...

Any suggestion will be helpful

Community
  • 1
  • 1
neeraj
  • 119
  • 1
  • 2
  • 7

1 Answers1

0

You can check if the role exists and add it if it doesn't like this:

var adminRoleExists = Roles.RoleExists(AppRoles.Admin.ToString());
if (!adminRoleExists)
{
    Roles.CreateRole(AppRoles.Admin.ToString());
}
Lzh
  • 3,585
  • 1
  • 22
  • 36