Though ASP.NET controls are great, in many cases I prefer the power and flexibility of doing things in code.
I wrote the following code this week as part of a demo I’m working on and thoght I would share it. (Why write all these case statements yourself.)
I hope someone finds it useful.
1: public partial class NewUser : System.Web.UI.Page
2: {
3: protected void Page_Load(object sender, EventArgs e)
4: {
5: Session["ContinueDestinationPageUrl"] = Request.QueryString["ReturnUrl"];
6: }
7:
8: protected void btnAddUser_Click(object sender, EventArgs e)
9: {
10: if (Page.IsValid)
11: {
12: MembershipCreateStatus createStatus;
13: string sUserName = tbUserName.Text;
14: string sPassword = tbPassword.Text;
15: string sEmail = tbEmailAddress.Text;
16: string sQuestion = tbSecurityQuestion.Text;
17: string sAnswer = tbSecurityAnswer.Text;
18:
19: MembershipUser newUser = Membership.CreateUser(
HttpUtility.HtmlEncode(sUserName.Trim()),
20: HttpUtility.HtmlEncode(sPassword.Trim()),
21: HttpUtility.HtmlEncode(sEmail.Trim()),
22: HttpUtility.HtmlEncode(sQuestion.Trim()),
23: HttpUtility.HtmlEncode(sAnswer.Trim()),
24: true,
25: out createStatus);
26: string CreateResultMessage = "";
27: switch (createStatus)
28: {
29: case MembershipCreateStatus.Success:
30: CreateResultMessage =
"» The user was successfully created. «";
31: break;
32: case MembershipCreateStatus.InvalidUserName:
33: CreateResultMessage =
"The user name was not found in the database.";
34: break;
35: case MembershipCreateStatus.InvalidPassword:
36: CreateResultMessage =
"The password is not formatted correctly.";
37: break;
38: case MembershipCreateStatus.InvalidQuestion:
39: CreateResultMessage =
"The password question is not formatted correctly.";
40: break;
41: case MembershipCreateStatus.InvalidAnswer:
42: CreateResultMessage =
"The password answer is not formatted correctly.";
43: break;
44: case MembershipCreateStatus.InvalidEmail:
45: CreateResultMessage =
"The e-mail address is not formatted correctly.";
46: break;
47: case MembershipCreateStatus.DuplicateUserName:
48: CreateResultMessage =
"The user name already exists in the database
for the application.";
49: break;
50: case MembershipCreateStatus.DuplicateEmail:
51: CreateResultMessage =
"The e-mail address already exists in the database
for the application.";
52: break;
53: case MembershipCreateStatus.UserRejected:
54: CreateResultMessage =
"The user was not created, for a reason defined
by the provider.";
55: break;
56: case MembershipCreateStatus.InvalidProviderUserKey:
57: CreateResultMessage =
"The provider user key is of an invalid type or
format.";
58: break;
59: case MembershipCreateStatus.DuplicateProviderUserKey:
60: CreateResultMessage =
"The ProviderUserKey already exists in the database
for the application.";
61: break;
62: case MembershipCreateStatus.ProviderError:
63: CreateResultMessage =
"The provider returned an error that is not described
by other ";
64: CreateResultMessage +=
"MembershipCreateStatus enumeration values.";
65: break;
66: }
67:
68: if (createStatus != MembershipCreateStatus.Success)
69: {
70: lblCreateResultMessage.CssClass = "ValidationError";
71: lblCreateResultMessage.Text = CreateResultMessage;
72: btnAddUser.Visible = true;
73: divResultMessage.Visible = true;
74: }
75: else
76: {
77: lblCreateResultMessage.CssClass = "bold";
78: btnAddUser.Visible = false;
79: FormsAuthentication.SetAuthCookie(newUser.UserName, false);
80:
81: string continueUrl =
Session["ContinueDestinationPageUrl"].ToString();
82: if (String.IsNullOrEmpty(continueUrl))
83: {
84: continueUrl = "~/";
85: }
86: Response.Redirect(continueUrl);
87: }
88:
89: }
90: }
91:
92: }
Filed under:
.NET





















RE: Code Snippet : Manually Creating a New ASP.NET Membership User in C#
Pingback from Twitter Trackbacks for Code Snippet : Manually Creating a New ASP.NET Membership User in C# : Misfit Geek [msjoe.com] on Topsy.com
RE: Code Snippet : Manually Creating a New ASP.NET Membership User in C#
Thank you for submitting this cool story – Trackback from DotNetShoutout
RE: Code Snippet : Manually Creating a New ASP.NET Membership User in C#
You’ve been kicked (a good thing) – Trackback from DotNetKicks.com
Hey Joe,
thanks for this, im just considering refactoring my sign up page. How about going the whole hog and providing us the markup too?
dont call me lazy ;-D
Thanks, Nice information.
learn more
My ASP skills are a bit outdated thanks for writing stuff like this.
Maybe to consider to put message in resx in order to support multi-language. It will replace the switch-case block.
ie: MembershipMessage..ResourceManager.GetString(createStatus);
Hi Joe, is there any plan to rewrite the ClassField starter kit with MVC?
Awsome article. Thanks
Jacob – what is the ClassField starter kit ?
Like the Resource idea, saves a lot of hard coding. Nice code example. Working on a site now and will be using the Membership provider so it’s great timing too!
Hi Joe, it is Classifieds Site Starter Kit, http://www.asp.net/…/classifieds
Thansk – I’ll have a look at it