在ASP.NET用户注册过程中MemberShipException [英] MemberShipException during the User Register at ASP.NET

查看:151
本文介绍了在ASP.NET用户注册过程中MemberShipException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是ASP.NET MVC 4模板来创建我的应用程序,在我的方法注册的帖子,我把那个叫用户配置并保存新的信息,在这种情况下,这将节省用户类型

I'm using the ASP.NET MVC 4 template to create my application, in my method Register POST, i put a block that call the UserProfile and save new information, on this case, it will save UserType.

问题是:每一次,我会保存用户,异常 MembershipCreateUserException 被称为并显示在页面上的以下信息:
用户名已经存在。请输入不同的用户名。
用户保存在数据库中。

The problem is: Every time that I'll save the user, the exception MembershipCreateUserException is called and the following message is displayed on the page: User name already exists. Please enter a different user name. The User is saved on Database.

这是我的方法注册:

public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user

                try
                {

                   // var user = new UserProfile() { UserName = model.UserName };

                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password);
                    return RedirectToAction("Index", "Home");

                }
                catch (MembershipCreateUserException e)

                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
//new code
                using (UsersContext db = new UsersContext())
                {

                    // Insert name into the profile table
                    UserProfile newUser = db.UserProfiles.Add(new UserProfile { UserName = model.UserName, UserType = model.UserType });
                    db.SaveChanges();

                }
            }

任何人都知道如何解决呢?每一个帮助将是有效的!谢谢你们。

Anybody knows how can i resolve it? Every help will be valid! Thanks guys.

推荐答案

下面一行添加一个新用户:

The following line adds a new user:

WebSecurity.CreateUserAndAccount(model.UserName, model.Password);

和这条线又做的:

UserProfile newUser = db.UserProfiles.Add(new UserProfile { UserName = model.UserName, UserType = model.UserType });

而不是尝试下面添加用户类型创建用户时,确保用户等级和积分是你RegisterModel模型的一部分​​:

Try instead the following to add the user type when creating the user, make sure UserType is part of your RegisterModel model:

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { UserType = model.UserType});

所以整个注册方法如下:

So the entire Register method will look like this:

public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user

                try
                {
                   WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { UserType = model.UserType });

                   return RedirectToAction("Index", "Home");

                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

这篇关于在ASP.NET用户注册过程中MemberShipException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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