使用默认成员身份时添加自定义登录表单
本文关键字:添加 自定义 登录 表单 身份 默认 成员 | 更新日期: 2023-09-27 18:26:36
我正在使用ASP 4.5 WebForms使用的默认成员资格提供程序(使用我自己的数据库),并且我在登录表单方面遇到了问题。我想制作一个自定义表单,因为我使用了一个花哨的CSS/HTML5代码,但使用默认控件时,我的Visual Studio会破坏它,我想在登录按键上执行一些自定义代码,同时维护Default Membership Provider已经带来的控制(如密码失败计数等)。我应该调用什么方法/过程,或者应该编写什么自定义代码,以便用户在编写用户名和密码时使用这些文本框的值进行登录?我试着尝试登录控制器的登录按钮,但它的范围太有限,表格仍然有点破坏设计。非常感谢您的帮助!
正如承诺的那样,这是我使用的完整代码,其中包括一些关于查询和其他内容的截断:
MembershipCreateStatus status;
string passwordQuestion = "";
string passwordAnswer = "";
if (Membership.RequiresQuestionAndAnswer)
{
passwordQuestion = txtQ.Text;
passwordAnswer = txtA.Text;
}
try
{
MembershipUser newUser = Membership.CreateUser(txtUserName.Text, txtPassword.Text,
txtEmail.Text, passwordQuestion,
passwordAnswer, true, out status);
if (newUser == null)
{
Label1.Visible = true;
Label1.Text = GetErrorMessage(status);
}
else
{
string connect4 = ConfigurationManager.ConnectionStrings["InternalConnection"].ConnectionString;
using (SqlConnection conn4 = new SqlConnection(connect4))
{
string query4 = "INSERT INTO [UserInfo] ([UserName], [ConfirmationToken], [Validated], [FullInfo], [EMail], [SocioNum], [Birthday], [PostalAddress], [Zip], [City], [State], [Phone], [Fullname], [SocialSecurityEncrypted], [JoinDate]) VALUES (@UserName, @ConfirmationToken, @Validated, @FullInfo, @EMail, @SocioNum, @Birthday, @PostalAddress, @Zip, @City, @State, @Phone, @Fullname, @SocialSecurityEncrypted, @JoinDate)";
SqlCommand cmd4 = new SqlCommand(query4, conn4);
));
string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random r = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 30; i++)
{
int idx = r.Next(0, 35);
sb.Append(s.Substring(idx, 1));
}
string token = Convert.ToString(sb);
cmd4.Parameters.AddWithValue("@ConfirmationToken", (token));
(parameters here)
cmd4.CommandTimeout = 240;
conn4.Open();
cmd4.ExecuteNonQuery();
conn4.Close();
Session["Fullname"] = txtNombre.Text;
Session["token"] = token;
}
//CUSTOM EMAIL CODE
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("servername");
mail.From = new MailAddress("fromhere@tothere.com");
mail.To.Add(txtEmail.Text);
mail.IsBodyHtml = true;
mail.Subject = "Subject";
mail.Body = "HTML CODE HERE while sending token for verification.";
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("user", "pass");
SmtpServer.EnableSsl = false;
SmtpServer.Send(mail);
Response.Redirect("~/Login.aspx");
}
}
catch
{
Label1.Visible = true;
Label1.Text = "Erro. Verify info..";
}
}
public string GetErrorMessage(MembershipCreateStatus status)
{
switch (status)
{
case MembershipCreateStatus.DuplicateUserName:
return ".";
case MembershipCreateStatus.DuplicateEmail:
return ".";
case MembershipCreateStatus.InvalidPassword:
return "Password invalid.";
case MembershipCreateStatus.InvalidEmail:
return "";
case MembershipCreateStatus.InvalidAnswer:
return "";
case MembershipCreateStatus.InvalidQuestion:
return "";
case MembershipCreateStatus.InvalidUserName:
return "Invalid";
case MembershipCreateStatus.ProviderError:
return "Error";
case MembershipCreateStatus.UserRejected:
return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
default:
return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
}
}
}
}
它注册了ASP.NET Default Membership提供程序,但我创建了第二个表以获取更多信息,还创建了一个ConfirmationToken,该表生成并通过电子邮件发送给用户,然后用户单击类似确认的按钮,一个确认页面读取URL参数,如果它进行了验证,就会在UserInfo表中打开布尔值。在实际的ASP成员身份登录之前的登录过程中,我有一个用户验证查询,用于验证用户是否已经验证,如果已经验证,则继续使用默认的ASP成员资格登录。
使用Membership类、MembershipUser类和Forms Authentication类创建自己的登录表单。登录控件本身并没有太多逻辑。
例如,要设置身份验证cookie,请使用FormsAuthentication.SetAuthCookie
。要验证密码,请使用MembershipProvider.ValidateUser
。要查看用户是否因多次尝试无效密码而被锁定,请使用MembershipUser.IsLockedOut
。