使用默认成员身份时添加自定义登录表单

时间:2013-08-06 20:45:11

标签: c# asp.net asp.net-membership

我正在使用ASP 4.5 WebForms使用的默认成员资格提供程序(使用我自己的数据库),我遇到了有关登录表单的问题。我想制作一个自定义的,因为我正在使用一个花哨的CSS / HTML5代码,但是使用我的Visual Studio的默认控件只是打破了它,我想在登录印刷机上执行一些自定义代码,同时还要保持控件默认成员资格提供程序已经带来(如密码失败计数等)。我应该调用哪种方法/流程或者我应该编写哪些自定义代码,以便在用户编写用户名和密码时使用这些文本框的值进行登录?我试着尝试登录控制器的登录按钮,但是它的范围太有限,而且表格仍然有点破坏了设计。非常感谢任何帮助!

As Promised这是我使用的完整代码,有关查询和内容的一些截断:

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 memebrship登录之前的登录过程中,我有一个用户验证查询,用于验证用户是否已经过验证,如果是,则继续使用默认的asp成员资格登录。

1 个答案:

答案 0 :(得分:1)

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

例如,要设置身份验证Cookie,请使用FormsAuthentication.SetAuthCookie。要验证密码,请使用MembershipProvider.ValidateUser。要查看用户是否被锁定了太多无效密码尝试,请使用MembershipUser.IsLockedOut