使用ASP.NET身份与网络形式提出如何使用用户名,而不是电子邮件验证 [英] Using ASP.NET Identity with web forms- how to validate with username instead of email

查看:503
本文介绍了使用ASP.NET身份与网络形式提出如何使用用户名,而不是电子邮件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个较早的问题更具体的职位。我见过一些职位对这一问题的答案和都没有工作过那些谁问的问题。

This is a more specific post of an earlier question. I have seen a number of posts with answers on this issue and none have worked for those who asked the question.

现在的问题是,使用.NET的身份,一个人如何使用验证用户名的网页表单(不MVC),而不是代替它是默认的电子邮件地址。我可以张贴一些code但说实话,我不是很确定发布哪些类或方法。

The question is, using .NET Identity, how does one use the username for validation on a web form (Not MVC) instead instead of the email address which is the default. I could post some code but honestly I am not exactly sure what class or method to post.

如果你能回答这个问题的荣誉给你,因为没有人可以计算出来。

If you can answer this question kudos to you because no one else can figure it out.

推荐答案

寻找到喜欢的东西创造经理自定义符号后(要做到这一点与MVC看到的 http://www.jamessturtevant.com/posts/ASPNET-Identity2.0-Custom-Database/ )和查看答案这个问题的MVC(见<一href=\"http://stackoverflow.com/questions/33069201/how-to-login-with-username-instead-of-email-in-mvc-identity\">How与&QUOT登录;用户名&QUOT;代替&QUOT;电子邮件和QUOT;在MVC身份),事实证明,答案是令人尴尬的简单结果。
步骤如下:结果
修改Register.aspx和结果的Login.aspx
在这两个页面创建一个用户名表单控件。需要注意的是外地用户名已存在于数据库中,这样你就不必添加。只需复制当前电子邮件的形式控制和复制粘贴到表单。然后在新的表单控件更改以下。结果
1.更改电子邮件到用户名无处不在它出现在新形式下的控制结果
2.更改ID =电子邮件为ID =username的结果
3.修改的TextMode =电子邮件来的TextMode =单行结果
4.注释掉如下RequiredFieldValidator控件:结果
    的RequiredFieldValidator =服务器的ControlToValidate =电子邮件
     的CssClass =TEXT-危险的ErrorMessage =是必需的电子邮件领域。结果

After looking into things like creating a custom sign in manager (To do this with MVC see http://www.jamessturtevant.com/posts/ASPNET-Identity2.0-Custom-Database/) and viewing answers to this question for MVC ( See How to login with "UserName" instead of "Email" in MVC Identity?), it turns out that the answer is embarrassingly simple.
The Steps are as follows.
Modify Register.aspx and Login.aspx
On both pages create a UserName form control. Note that the field UserName already exists in the database so you do not have to add it. Just copy the current email form control and paste the copy back into the form. Then on the new form control change the following.
1. Change Email to UserName everywhere it appears in the new form control
2. Change ID="Email" to ID="UserName"
3. Change TextMode="Email" to TextMode="singleline"
4. Comment out the RequiredFieldValidator control that follows:
RequiredFieldValidator runat="server" ControlToValidate="Email" CssClass="text-danger" ErrorMessage="The email field is required."

在Register.aspx.cs:结果
更改用户VAR =新ApplicationUser(){用户名= Email.Text ,电子邮件= Email.Text};结果
要结果
VAR用户=新ApplicationUser(){用户名= UserName.Text 电子邮件= string.IsNullOrEmpty(Email.Text)? xxx@x.com:Email.Text }; (这是一个黑客位,但它的工作原理。)搜索结果

On Register.aspx.cs:
Change var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text };
To
var user = new ApplicationUser() { UserName = UserName.Text, Email = string.IsNullOrEmpty(Email.Text)? "xxx@x.com" : Email.Text }; (This is a bit of a hack but it works.)

在Login.aspx.cs结果
更改结果
VAR的结果= signinManager.PasswordSignIn( Email.Text ,Password.Text,RememberMe.Checked,shouldLockout:假);结果
要结果
VAR的结果= signinManager.PasswordSignIn( UserName.Text ,Password.Text,RememberMe.Checked,shouldLockout:假);搜索结果

On Login.aspx.cs
Change
var result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: false);
To
var result = signinManager.PasswordSignIn(UserName.Text, Password.Text, RememberMe.Checked, shouldLockout: false);

在IdentityConfig.cs结果
更改结果
RequireUniqueEmail = 真正结果
要结果
RequireUniqueEmail =

On IdentityConfig.cs
Change
RequireUniqueEmail = true
To
RequireUniqueEmail = false

这篇关于使用ASP.NET身份与网络形式提出如何使用用户名,而不是电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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