Simplemembership - 添加电子邮件领域,作为登录名 [英] Simplemembership - adding email field and use as login-name
问题描述
我想在这里做两件事情:
1 添加电子邮件字段(默认)用户配置表中。
奇迹般有效。
用户可以注册了用户名和电子邮件。
1. Adding an email field to the (default)UserProfile table. Works like a charm. Users can register both a username and email.
2 更改使用的电子邮件,而不是用户名登录,但这也就像一个魅力。
2. Change the login to use the email instead of username, this does also works like a charm.
下面是问题所在。以上只能当我它们分开,只要我使用他们两个的注册过程失败有以下的错误信息:
Here is the problem. The above only works when i seperate them, as soon as I use both of them the registration process fails with the following error message:
Cannot insert the value NULL into column 'UserName', table 'opdb.dbo.UserProfile'; column does not allow nulls. INSERT fails.
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Email = model.Email
});
WebSecurity.Login(model.Email, model.Password);
我或多或少继
导这里找到 一>
在评论部分我看见有同样的问题别人,但是,解决的办法是不设置用户名现场为允许空值作为答复说明。
I am more or less following the guide found here, In the comments section I see others having the same problem, however, the solution is not to set the UserName field to allow nulls as stated in the replies.
但奇怪的是,一切正常,只要我不使用这两个在一起。有任何想法吗?这让我发疯!
The strange thing is that everything works as long as I don't use both together. Any ideas? This drives me crazy!
在此先感谢
修改:它可以在数据库中列的顺序?与用于识别领域需要在表中的第一列?所以,如果我想电子邮件作为IDENT需要在表中的第一列?
Edit: Could it be the order of columns in the database? That the field used for identification needs to be the first column in the table? So if I want email as ident that need to be the first column in the table?
推荐答案
这不是列的顺序。我想你没有设置你的 WebSecurity.InitializeDatabaseConnection
正确。这听起来像当你添加电子邮件字段,它被作为电子邮件字段(而不是为用户自然键)处理,当你更改登录使用的电子邮件时,实际上是插入电子邮件到username列?
It is not the column order. I think you haven't set your WebSecurity.InitializeDatabaseConnection
up properly. It sounds like when you add the email field, it is treated as the email field (and not the natural key for the users), and when you change the login to use the email, you are actually inserting the email into the UserName column?
无论哪种方式,它是简单的使工作。假设你已经在你的电子邮件列在数据库中,您使用的是标准的Internet应用程序模板,请执行以下操作:
Either way, it is simple to make work. Assuming you already have your email column in the database and you are using the standard Internet Application template, do the following:
- 更改
用户配置
模型添加一个电子邮件
属性,做同样的RegisterModel
并改变从用户名
的登录
模式电子邮件
- 更新
InitializeSimpleMembershipAttribute
使用电子邮件,而不是用户名
作为用户自然键 - 更新
的AccountController
登录
和注册
操作,以便您捕获用户名和注册电子邮件,并使用电子邮件在登录 - 更新视图
- Change your
UserProfile
model to add anEmail
property, do the same to theRegisterModel
and change theLogin
model fromUserName
toEmail
- Update the
InitializeSimpleMembershipAttribute
to use Email instead ofUserName
as the natural key for the users - Update the
AccountController
Login
andRegister
actions so you capture a UserName and an Email on registration, and use the Email on login - Update the views
所以,实际的修改,使有:
So, the actual changes to make are:
- 型号/ AccountModels.cs变化
-
类用户配置
:添加属性公共字符串电子邮件{获得;组; }
-
类LoginModel
:从更改属性名用户名
到电子邮件
-
类RegisterModel
:添加属性公共字符串电子邮件{获得;组; }
,并设置其属性为要求
,将(名称
显示等。(最大长度如果要强制执行,以及
- Models/AccountModels.cs changes
class UserProfile
: add the propertypublic string Email { get; set; }
class LoginModel
: change the property name fromUserName
toEmail
class RegisterModel
: add the propertypublic string Email { get; set; }
and set its attributes toRequired
, set theDisplay(Name
etc. (max length if you want to enforce that as well
-
登录
方法:- 更改
model.UserName
到model.Email
在该行如果(ModelState中。 IsValid的&功放;&安培; WebSecurity.Login(model.UserName ...
Login
method:- Change
model.UserName
tomodel.Email
in the lineif (ModelState.IsValid && WebSecurity.Login(model.UserName ...
- 更改
WebSecurity.CreateUserAndAccount(model.UserName,model.Password);
到WebSecurity.CreateUserAndAccount(model.Email,model.Password,新{用户名= model.UserName});
- 修改 - 错过了一个变化:
更改WebSecurity.Login(model.UserName,model.Password);
到WebSecurity.Login(model.Email,model.Password);
- Change
WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
toWebSecurity.CreateUserAndAccount(model.Email, model.Password, new { UserName = model.UserName });
- EDIT - missed a change:
Change
WebSecurity.Login(model.UserName, model.Password);
toWebSecurity.Login(model.Email, model.Password);
- 更改
在
WebSecurity.InitializeDatabaseConnection(DefaultConnection用户名
到电子邮件
,用户配置,用户ID,用户名,autoCreateTables:TRUE);
- Change
"UserName"
to"Email"
inWebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
-
m.UserName
三种用途更改为m.Email
- Change the three usages of
m.UserName
tom.Email
- 复制并粘贴用户名
<李>
部分,然后更改重复的m.UserName
到m.Email
- Copy and paste the UserName
<li>
section and change the duplicatesm.UserName
tom.Email
在香草互联网应用MVC 4项目,这些变化将很好地工作(在一个真正的香草项目,您可能需要应用迁移,如果你想利用这些,你是不是编辑数据库中添加电子邮件列)。
In a vanilla Internet Application MVC 4 project, these changes will work perfectly (in a true vanilla project you may have to apply migrations if you want to use these and you aren't editing the database to add the Email column).
这篇关于Simplemembership - 添加电子邮件领域,作为登录名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Change
- 更改
-