添加用户角色注册(表单验证)MVC3 [英] Add User Roles on Registration (Forms Authentication) MVC3

查看:100
本文介绍了添加用户角色注册(表单验证)MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个MVC 3项目,希望将用户添加到角色,当他们注册,使用窗体身份验证。所以我想创造一些复选框,或者下拉列表中显示的角色,这是选择,用户被分配到,因为它们注册的角色。

我有这样的code,到目前为止,其工作方式:

 公众的ActionResult寄存器()
    {
        计算机[角色名] =新的SelectList(Roles.GetAllRoles(),角色名);
        返回查看();
    }

和在视图中我有:

 <标签=角色名>选择角色:LT; /标签>
@ Html.DropDownList(角色名)
@ Html.ValidationMessage(角色名)

这是HttpPost控制器的部分,这是我不知道的部分怎么code:

  [HttpPost]
    公众的ActionResult寄存器(RegisterModel模型)
    {
        如果(ModelState.IsValid)
        {
            //试图注册用户
            MembershipCreateStatus createStatus;
            Membership.CreateUser(model.UserName,model.Password,model.Email,NULL,NULL,真实,空出createStatus);            如果(createStatus == MembershipCreateStatus.Success)
            {
                FormsAuthentication.SetAuthCookie(model.UserName,假/ * * createPersistentCookie /);
                返回RedirectToAction(指数,家);
            }
            其他
            {
                ModelState.AddModelError(,错误codeToString(createStatus));
            }
        }        //如果我们走到这一步,事情失败了,重新显示形式
        返回查看(模型);

所以,所有的角色都在视图中显示出来。我需要知道什么是添加到HttpPost节得到这个工作。

非常感谢,艾米


解决方案

 如果(createStatus == MembershipCreateStatus.Success)
            {
                Roles.AddUserToRole(model.UserName角色名);
                FormsAuthentication.SetAuthCookie(model.UserName,假/ * * createPersistentCookie /);
                返回RedirectToAction(指数,家);
            }

试试这个。它应该工作

I am developing an MVC 3 project and want to add a user to a role when they are registered, using Forms Authentication. So I'd like to create some check boxes, or a drop down list showing the roles, which are selected and the user is assigned to the role as they are registered.

I have this code so far, which works:

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

And in the view I have:

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

This is HttpPost section of the controller, and this is the part that I don't know what to code:

    [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)
            {
                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);

So, all the roles do show up in the view. All I need to know is what to add to the HttpPost section to get this working.

Thanks a lot, Amy

解决方案

if (createStatus == MembershipCreateStatus.Success)
            {
                Roles.AddUserToRole(model.UserName, "RoleName");
                FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                return RedirectToAction("Index", "Home");
            }

Try this. It's should work

这篇关于添加用户角色注册(表单验证)MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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