使用 MemberShip 进行手工注册

虽然控件很好用,但代码更加灵活,不使用控件进行注册的方法:

Code:
  1. public partial class NewUser : System.Web.UI.Page  
  2.   
  3.   {
  4. protected void Page_Load(object sender, EventArgs e)  
  5.   
  6. {  
  7.   
  8. Session["ContinueDestinationPageUrl"] = Request.QueryString["ReturnUrl"];  
  9.   
  10. }  
  11.   
  12.   
  13. protected void btnAddUser_Click(object sender, EventArgs e)  
  14.   
  15. {  
  16.   
  17. if (Page.IsValid)  
  18.   
  19. {  
  20.   
  21. MembershipCreateStatus createStatus;  //注册状态
  22.   
  23. //获取注册信息
  24. string sUserName = tbUserName.Text;  
  25.   
  26. string sPassword = tbPassword.Text;  
  27.   
  28. string sEmail = tbEmailAddress.Text;  
  29.   
  30. string sQuestion = tbSecurityQuestion.Text;  
  31.   
  32. string sAnswer = tbSecurityAnswer.Text;  
  33.   
  34.   
  35. MembershipUser newUser = Membership.CreateUser(  
  36. HttpUtility.HtmlEncode(sUserName.Trim()),  
  37.   
  38. HttpUtility.HtmlEncode(sPassword.Trim()),  
  39.   
  40. HttpUtility.HtmlEncode(sEmail.Trim()),  
  41.   
  42. HttpUtility.HtmlEncode(sQuestion.Trim()),  
  43.   
  44. HttpUtility.HtmlEncode(sAnswer.Trim()),  
  45.   
  46. true,  
  47.   
  48. out createStatus);  
  49.   
  50. string CreateResultMessage = "";  
  51.   
  52. switch (createStatus)  //根据返回状态进行信息提示
  53.   
  54. {  
  55.   
  56. case MembershipCreateStatus.Success:  
  57.   
  58. CreateResultMessage = "» The user was successfully created. «";  
  59.   
  60. break;  
  61.   
  62. case MembershipCreateStatus.InvalidUserName:  
  63.   
  64. CreateResultMessage = "The user name was not found in the database.";  
  65.   
  66. break;  
  67.   
  68. case MembershipCreateStatus.InvalidPassword:  
  69.   
  70. CreateResultMessage = "The password is not formatted correctly.";  
  71.   
  72. break;  
  73.   
  74. case MembershipCreateStatus.InvalidQuestion:  
  75.   
  76. CreateResultMessage = "The password question is not formatted correctly.";  
  77.   
  78. break;  
  79.   
  80. case MembershipCreateStatus.InvalidAnswer:  
  81.   
  82. CreateResultMessage = "The password answer is not formatted correctly.";  
  83.   
  84. break;  
  85.   
  86. case MembershipCreateStatus.InvalidEmail:  
  87.   
  88. CreateResultMessage = "The e-mail address is not formatted correctly.";  
  89.   
  90. break;  
  91.   
  92. case MembershipCreateStatus.DuplicateUserName:  
  93.   
  94. CreateResultMessage = "The user name already exists in the database  
  95. for the application.";  
  96.   
  97. break;  
  98.   
  99. case MembershipCreateStatus.DuplicateEmail:  
  100.   
  101. CreateResultMessage = "The e-mail address already exists in the database  
  102. for the application.";  
  103.   
  104. break;  
  105.   
  106. case MembershipCreateStatus.UserRejected:  
  107.   
  108. CreateResultMessage = "The user was not created, for a reason defined  
  109. by the provider.";  
  110.   
  111. break;  
  112.   
  113. case MembershipCreateStatus.InvalidProviderUserKey:  
  114.   
  115. CreateResultMessage = "The provider user key is of an invalid type or  
  116. format.";  
  117.   
  118. break;  
  119.   
  120. case MembershipCreateStatus.DuplicateProviderUserKey:  
  121.   
  122. CreateResultMessage = "The ProviderUserKey already exists in the database  
  123. for the application.";  
  124.   
  125. break;  
  126.   
  127. case MembershipCreateStatus.ProviderError:  
  128.   
  129. CreateResultMessage = "The provider returned an error that is not described  
  130. by other ";  
  131.   
  132. CreateResultMessage += "MembershipCreateStatus enumeration values.";  
  133.   
  134. break;  
  135.   
  136. }  
  137.   
  138.   
  139. if (createStatus != MembershipCreateStatus.Success)  //失败返回提示信息
  140.   
  141. {  
  142.   
  143. lblCreateResultMessage.CssClass = "ValidationError";  
  144.   
  145. lblCreateResultMessage.Text = CreateResultMessage;  
  146.   
  147. btnAddUser.Visible = true;  
  148.   
  149. divResultMessage.Visible = true;  
  150.   
  151. }  
  152.   
  153. else  //成功依然给出提示信息,并转向returnURL
  154.   
  155. {  
  156.   
  157. lblCreateResultMessage.CssClass = "bold";  
  158.   
  159. btnAddUser.Visible = false;  
  160.   
  161. FormsAuthentication.SetAuthCookie(newUser.UserName, false);  
  162.   
  163.   
  164. string continueUrl = Session["ContinueDestinationPageUrl"].ToString();  
  165.   
  166. if (String.IsNullOrEmpty(continueUrl))  
  167.   
  168. {  
  169.   
  170. continueUrl = "~/";  
  171.   
  172. }  
  173.   
  174. Response.Redirect(continueUrl);  
  175.   
  176. }  
  177. }
  178.   
  179. }  
  180.   
  181. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值