ASP.net 2.0 role and member management

Source: Internet
Author: User
Tags config contains count mail modify alphanumeric characters
asp.net

Member Management
First, Membership class: Create and delete users, retrieve user letters, generate random passwords, login verification
Create new User: try {
Membership.CreateUser ("name", "Password", "Mail");
}
catch (MembershipCreateUserException e)
{
Failed
Switch (E.statuscode)
{
Case Membershipcreatestatus.duplicateusername:

Case Membershipcreatestatus.duplicateemail:

Case Membershipcreatestatus.invalidpassword:

Default

}
Logon authentication:
if (Membership.ValidateUser (Username.text, Password.text))
FormsAuthentication.RedirectFromLoginPage (Username.text,
rememberme.checked); Methods: CreateUser (Create user) DeleteUser (delete user) Generatepassword (production then password) getallusers (get user) GetUser (view a user) UpdateUser (modify user) Val Idateuser (validation is successful)
Second, MembershipUser class:
Describes a single registered user information in the member data store
Contains a number of properties to get and set user information
Contains methods to retrieve, alter, and reset passwords
Return values through properties such as GetUser and CreateUser
Properties: Comment creationdate Email lastlogindate lastpasswordchangeddate UserId UserName
Methods: ChangePassword changepassword-questionandanswer GetPassword ResetPassword
Suspend logon rights:
if (Membership.ValidateUser (Username.text, Password.text)) {
MembershipUser user = Membership.getuser (username.text);
User.comment = "0"; Record number of logons
RedirectFromLoginPage (Username.text, rememberme.checked);
}
else {
MembershipUser user = Membership.getuser (username.text);
if (user!= null) {
String count = Convert.ToInt32 (user.comment) + 1;
User.comment = count. ToString ();
}
Three uses SQL Server provider
<configuration>
<system.web>
<membership defaultprovider= "AspNetSqlProvider"/>
</system.web>
</configuration> Change provider configuration:
<membership>
<providers>
<remove name= "AspNetSqlProvider"/>
<add name= "AspNetSqlProvider"
Type= "System.Web.Security.SqlMembershipProvider, system.web,"
Connectionstringname= "Remotesqlserver"
Enablepasswordretrieval= "false"
Enablepasswordreset= "true"
Requiresquestionandanswer= "false"
Applicationname= "/"
Requiresuniqueemail= "false"
Passwordformat= "Hashed"
Description= "Stores and retrieves membership data"
/>
</providers>
</membership> configuration Provider
Member providers support many configuration options, how passwords are stored (plaintext, hashing, encrypting)? Is the password allowed to be restored? Does the user have to have a unique e-mail address? is represented by the provider class property, which is initialized in the configuration file.
Iv. Role Management
Method: Addusertorole createrole deleterole getrolesforuser (View user roles) Getusersinrole IsUserInRole
Create a new role
if (! Roles.roleexists ("Developers")) {
Roles.createrole ("developers");
Add user to a role
String name = Membership.getuser (). Username;
Roles.addusertorole (Name, "developers"); Configure Web.config enable roles
<configuration>
<system.web>
<rolemanager enabled= "true"/>
</system.web>
</configuration> Enable role caching
<configuration>
<system.web>
<rolemanager enabled= "true" cacherolesincookie= "true"/>
<!--other rolemanager attributes (and their defaults) include:
Cookiename= ". Aspxroles "//Cookie Name
cookietimeout= "//Cookie Lifetime
Cookiepath= "/"//Cookie Path
Cookierequiressl= "false"//Restrict cookie to SSL?
Cookieslidingexpiration= "true"//renew expiring cookies?
Createpersistentcookie= "false"//Issue persistent cookie?
cookieprotection= ' all '/>//Cookie protection level
-->
</system.web>
</configuration> using SQL Server providers
<configuration>
<system.web>
<rolemanager enabled= "true" defaultprovider= "AspNetSqlRoleProvider"/>
</system.web>
</configuration>
Error Tip:
The minimum password length is 7, which must contain the following non-alphanumeric characters: 1

Solution:
Received the above message is mainly in the creation of users, when the use of ASP.net Web site management tools to create users will also be generated.
The main reason is that the password input does not meet the requirements, to change the above provisions, there are two main methods:
1. All sites are changed.
Find Machine.config file

<membership>
<providers>
<add name= "AspNetSqlMembershipProvider"
Type= "System.Web.Security.SqlMembershipProvider, system.web, version=2.0.0.0, Culture=neutral, publickeytoken= B03F5F7F11D50A3A "
Connectionstringname= "LocalSqlServer"
Enablepasswordretrieval= "false"
Enablepasswordreset= "true"
Requiresquestionandanswer= "true"
Applicationname= "/"
Requiresuniqueemail= "false"
Passwordformat= "Hashed"
Maxinvalidpasswordattempts= "5"
Minrequiredpasswordlength= "7"
minrequirednonalphanumericcharacters= "1"
passwordattemptwindow= "10"
Passwordstrengthregularexpression= ""/>
</providers>
</membership>
There are two attributes, one is minrequiredpasswordlength, meaning the longest password, the default is 7 and the other is minRequiredNonalphanumericCharacters, the default is 1, meaning there is at least one non-alphanumeric character, Just change it to 0.

2. If only to a certain site, just modify the value of Web.config OK
To modify the above, insert the above code in the <system.web> below OK.

To change to a password rule is "at least 6 characters without special characters," as follows:
(Note: Be sure to add <remove name= "AspNetSqlMembershipProvider"/>, or you will be prompted "item" AspNetSqlMembershipProvider "added" error message)

<membership>
<providers>
<remove name= "AspNetSqlMembershipProvider"/>
<add name= "AspNetSqlMembershipProvider"
Type= "System.Web.Security.SqlMembershipProvider, system.web, version=2.0.0.0, Culture=neutral, publickeytoken= B03F5F7F11D50A3A "
Connectionstringname= "LocalSqlServer"
Enablepasswordretrieval= "false"
Enablepasswordreset= "true"
Requiresquestionandanswer= "true"
Applicationname= "/"
Requiresuniqueemail= "false"
Passwordformat= "Hashed"
Maxinvalidpasswordattempts= "5"
Minrequiredpasswordlength= "6"
minrequirednonalphanumericcharacters= "0"
passwordattemptwindow= "10"
Passwordstrengthregularexpression= ""/>
</providers>
</membership>



Related Article

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.