将用户添加到角色上在注册失败ASP.NET MVC3 [英] Adding users to roles on registring failed ASP.NET MVC3

查看:202
本文介绍了将用户添加到角色上在注册失败ASP.NET MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢让用户能够选择asp.net MVC Web应用程序及其对寄存器的作用。我有previous通过asp.net addministration工具添加角色。这里是code。我正在使用寄存器方法

I like to enable users to choose their role on register in asp.net mvc web application. I have previous add roles via asp.net addministration tool. Here is the code I am using in register methods

public ActionResult Register()  
{
ViewData["roleName"] = new SelectList(Roles.GetAllRoles(), "roleName");
return View();
}


 [HttpPost]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // 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, "roleName");
                FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", ErrorCodeToString(createStatus));
            }
        }

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

而在注册查看我得到这个code

While in Register View I got this code

    <label for="roleName">Select Role:</label>
         @Html.DropDownList("roleName")
         @Html.ValidationMessage("roleName")

角色都写在数据库和用户虽然。然而UsersInRoles表是空的,我得到这个例外

Roles are written in the database and users although. However UsersInRoles table is empty and I got this exception

System.Configuration.Provider.ProviderException:角色角色名未找到

System.Configuration.Provider.ProviderException: The role 'roleName' was not found.

有谁有同样的问题?

推荐答案

您应该添加角色名参数来注册,获得用户角色名选择控制器动作,或者从检索的FormCollection角色名,这里是例子
你注册后行动更改为:

You should add roleName parameter to Register controller action for get selected by user roleName, or retrieve roleName from FormCollection, here is example Change your Register post action to:

        [HttpPost]
        public ActionResult Register(RegisterModel model, String roleName)
        {
            if (ModelState.IsValid)
            {
                // 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, roleName);
                    FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

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

这篇关于将用户添加到角色上在注册失败ASP.NET MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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