1

我正在使用 ASP 4.5 WebForms 使用的默认成员资格提供程序(使用我自己的数据库),并且我遇到了有关登录表单的问题。我想做一个自定义的,因为我使用了一个花哨的 CSS/HTML5 代码,但是使用给定我的 Visual Studio 的默认控件只会破坏它,我想在登录按下时执行一些自定义代码,同时还维护控件默认成员资格提供者已经带来(如密码失败计数等)。我应该调用什么方法/过程或者我应该编写什么自定义代码,以便当用户编写用户名和密码时,它使用这些文本框的值进行登录?我尝试涉足登录控制器的登录按钮,但它的范围太有限,表格仍然有点破坏设计。任何帮助是极大的赞赏!

正如所承诺的,这是我使用的完整代码,其中包含一些关于查询和内容的截断:

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 默认成员资格提供程序,但我创建了第二个表以获取更多信息,以及生成并通过电子邮件发送给用户的 ConfirmationToken,然后用户单击确认,确认页面读取 URL参数,如果它验证它会打开 UserInfo 表中的布尔值。在实际 ASP 会员登录之前的登录过程中,我有一个用户验证查询,用于验证用户是否已经过验证,如果是,则继续使用默认的 asp 会员登录。

4

1 回答 1

1

使用Membership 类MembershipUser 类Forms Authentication 类并创建您自己的登录表单。登录控件本身并没有太多的逻辑。

例如,要设置身份验证 cookie,请使用FormsAuthentication.SetAuthCookie. 要验证密码,请使用MembershipProvider.ValidateUser. 要查看用户是否因过多的无效密码尝试而被锁定,请使用MembershipUser.IsLockedOut.

于 2013-08-06T20:49:44.103 回答