Asp.net membership FAQs

Source: Internet
Author: User

1. login verificationCode

If (Membership. validateuser (username. Text, password. Text ))
{
If (Request. querystring [ " Returnurl " ] ! = Null )
{
Formsauthentication. redirectfromloginpage (username. Text,False);
}
Else
{
Formsauthentication. setauthcookie (username. Text,False);
}
}
Else
{
Response. Write ("Invalid userid and password");
}

2. Registration Code (via profile extension)

 

Membershipcreatestatus MS;
Membershipuser newuser = Membership. createuser (textbox1.text, textbox2.text, textbox4.text, textbox5.text, textbox6.text, True , Out MS );
  If MS = Membershipcreatestatus. Success)
  {
Profilecommon P = (Profilecommon) profilecommon. Create (newuser. username, True );
Roles. addusertorole (textbox1.text, " Usermember " );
P. telphone = Textbox7.text. Trim ();
P. qq = Textbox8.text. Trim ();
P. Address = Textbox9.text. Trim ();
P. Save ();
Roles. addusertorole (textbox1.text, " Usergroup " ); // Add a new user to the usergroup role Group
Server. Transfer ( " Login. aspx " );
}
Else
{
StringErrorcode;
 SwitchMS)
{
 CaseMembershipcreatestatus. duplicateusername:
Errorcode= "Username already exists.";
Break;
}
}

The configuration of the profile node in the corresponding web. config is as follows;

< Profile Enabled = "True" Defaultprovider = "Profilesqlmembershipprovider" >
< Providers >
< Add Name = "Profilesqlmembershipprovider"  
Type = "System. Web. profile. sqlprofileprovider"  
Connectionstringname = "Webshopconnectionstring" />
</ Providers >
< Properties >
< Add Name = "Telphone" Type = "String" />
< Add Name = "QQ" Type = "String" />
< Add Name = "Address" Type = "String" />
</ Properties >
</ Profile >

4. Verify that the user name has code

Membershipusercollection col = Membership. findusersbyname ( This . Textbox1.text );
If (Col. Count = 0 )
Label1.text = " Congratulations! You can register " ;
Else
Label1.text = " This user already exists " ;

5. Determine whether a user logs in or uses anonymous code

If (Httpcontext. Current. User. Identity. isauthenticated = True )
{
Label1.text= "You are a registered user. Welcome to register";
}
Else
{
Label1.text= "You are an anonymous user. Please register";
}

6. Get the current user name

Membershipuser U;
U=Membership. getuse (user. Identity. Name );
Label1.text=U. Username;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.