如何使用“用户名”登录而不是“电子邮件地址”;在ASP.Net Web表单身份
本文关键字:ASP Web 身份 表单 电子邮件地址 Net 用户 何使用 用户名 登录 | 更新日期: 2023-09-27 18:04:44
我需要将我的登录设置为使用用户名而不是电子邮件地址,我该如何更改?我到处看,但他们是MVC代码不是web表单。
我很感激你为解决我的问题所做的努力。
using System;
using System.Web;
using System.Web.UI;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Owin;
using Web_WebApp.Models;
namespace Web_WebApp.Account
{
public partial class Login : Page
{
protected void Page_Load(object sender, EventArgs e)
{
RegisterHyperLink.NavigateUrl = "Register";
// Enable this once you have account confirmation enabled for password reset functionality
//ForgotPasswordHyperLink.NavigateUrl = "Forgot";
OpenAuthLogin.ReturnUrl = Request.QueryString["ReturnUrl"];
var returnUrl = HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
if (!String.IsNullOrEmpty(returnUrl))
{
RegisterHyperLink.NavigateUrl += "?ReturnUrl=" + returnUrl;
}
}
protected void LogIn(object sender, EventArgs e)
{
if (IsValid)
{
// Validate the user password
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
// This doen't count login failures towards account lockout
// To enable password failures to trigger lockout, change to shouldLockout: true
var result = signinManager.PasswordSignIn(UserName.Text, Password.Text, RememberMe.Checked, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
break;
case SignInStatus.LockedOut:
Response.Redirect("/Account/Lockout");
break;
case SignInStatus.RequiresVerification:
Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}",
Request.QueryString["ReturnUrl"],
RememberMe.Checked),
true);
break;
case SignInStatus.Failure:
default:
FailureText.Text = "Invalid login attempt";
ErrorMessage.Visible = true;
break;
}
}
}
}
}
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Owin;
using Web_WebApp.Models;
namespace Web_WebApp.Account
{
public partial class Register : Page
{
protected void CreateUser_Click(object sender, EventArgs e)
{
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
var user = new ApplicationUser() { UserName = UserName.Text, Email = Email.Text, FirstName = FirstName.Text, MiddleName = MiddleName.Text, LastName = LastName.Text };
IdentityResult result = manager.Create(user, Password.Text);
if (result.Succeeded)
{
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
//string code = manager.GenerateEmailConfirmationToken(user.Id);
//string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request);
//manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href='"" + callbackUrl + "'">here</a>.");
signInManager.SignIn( user, isPersistent: false, rememberBrowser: false);
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}
}
}
您可以简单地通过用户名或您想要的任何内容找到用户,然后直接使用SignIn
方法登录用户:
protected void LogIn(object sender, EventArgs e)
{
if (IsValid)
{
// Validate the user password
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
// assuming you have a text box named UserName
var user=manager.FindByName(UserName.Text);
// or for advanced scenarios you can write:
// var user = manager.Users.FirstOrDefault(u => u.UserName == UserName.Text);
if(user !=null)
{
if(manager.CheckPassword(user, Password.Text))
{
signinManager.SignIn(user, false, RememberMe.Checked);
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
}
}
FailureText.Text = "Invalid login attempt";
ErrorMessage.Visible = true;
}
如果你看一下SignInManagerExtensions。在MSDN中,这将告诉您PasswordSignIn
方法中的第一个参数是userName
。
MVC,WebForms在注册电子邮件地址时默认设置为Email。您也可以更改注册操作,以允许注册用户名,这在默认情况下将电子邮件添加为用户名和电子邮件在数据库中。
protected void CreateUser_Click(object sender, EventArgs e)
{
// ...
// UserName and Email are both set to Email.text
// Add a UserName TextBox and Replace UserName = Email.Text with UserName = UserName.Text
var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text };
// ...
}
protected void LogIn(object sender, EventArgs e)
{
if (IsValid)
{
// ...
// The first argument is userName
// Replace mail.Text with UserName.Text
var result = signinManager.PasswordSignIn(mail.Text, Password.Text, RememberMe.Checked, shouldLockout: false);
// ..
}
}
替换:
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="Email" CssClass="col-md-2 control-label">Email</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="Email" CssClass="form-control" TextMode="Email" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="Email"
CssClass="text-danger" ErrorMessage="The email field is required." />
</div>
</div>
:
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="UserName" CssClass="col-md-2 control-label">UserName</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="UserName" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="UserName"
CssClass="text-danger" ErrorMessage="The username field is required." />
</div>
</div>