ASP. NET 2.0 member management and role management

Source: Internet
Author: User
Tags connectionstrings

1.Member Service

UseASP. NET web site administration toolCreate and configure website permissions. This tool is available inHttp: // localhost/<Project name>/Webadmin. axd,Through configuration, the tool will generateWeb. configIs used to store member information.

InASP. net2.0Two classes are also provided for member management.MembershipAndMembershipuser,They are locatedSystem. Web. SecurityUnder the namespace. The former contains a series of static methods for creation, deletion, modification, verification, and other operations. The latter is a class that describes the login information of a single user. Each instance is a collection of login information.

MembershipMethods:(You can see the name and description.)

    • createuser
    • deleteuser
    • generatepassword
    • getalluser
    • getuser
    • updateuser
    • validateuser

MembershipuserMethods:

    • Changepassword
    • Changepasswordquestionandanswer
    • GetPassword
    • Resetpassword

MembershipuserSome attributes of: (you can see the name and meaning)

    • comment (used to store user-defined data)
    • creationdate
    • email
    • lastlogindate
    • lastpasswordchangeddate
    • userid
    • username

 

Some examples:

Create a new user

 

Membershipcreatestatus status;
Membershipuser user = Membership. createuser ( This . Tb_username.text,
This . Tb_password.text, This . Tb_email.text, Out Status ); // The output parameter status contains the User Creation result information, which can be further processed.

Change Password

 

If ( This . Isvalid)
{
Membershipuser user = Membership. getuser ();

User. Email =   This . Tb_email.text;
User. Comment =   This . Tb_comment.text;
Membership. updateuser (User );

If (( This . Tb_oldpassword.text.length >   0 ) &&
( This . Tb_newpassword.text.length >   0 ))
{
User. changepassword (This. Tb_oldpassword.text,
This. Tb_newpassword.text );
}
}

All other user information can be stored in SQL Server Medium or Access . To SQL Server You can use <WINDIR> \ Microsoft. NET \ framework \ <version> Under Aspnet_regsql.exeTool to generate a database. For example

After the database is generated, make the following changes in the configuration file:

 

<? XML version = "1.0" ?>
< Configuration >

< Connectionstrings >
< Add Name = "Localsqlserver"
Connectionstring = "Data Source = 127.0.0.1; Integrated Security = sspi"   />
</ Connectionstrings >

< System . Web >
< Membership Defaultprovider = "Aspnetsqlprovider"   />
< Rolemanager Enabled = "True" Defaultprovider = "Aspnetsqlprovider"   />
</ System. Web >
</ Configuration >

 

2.Role Management Service

Similar to member management, role management can also passASP. NET web site administration tool. Unlike Member Management, role management only has one class.Roles. That is to say, a role only exists as a string and does not have a role class to record its member information.

It provides a series of static methods for role operations. (Similarly, it is basically known by name)

      • Addusertorole
      • Createrole
      • Deleterole
      • Getrolesforuser
      • Getusersinrole
      • Isuserinrole
      • Removeuserfromrole

Because role management is disabled by default, make the following changes in the configuration file:

 

< Configuration >
< System . Web >
< Rolemanager Enabled = "True"   />
</ System. Web >
</ Configuration >

You can also cache role information in cookies. To achieve this, you only need to make the following changes in the configuration file: < Configuration >
< System . Web >
< Rolemanager Enabled = "True" Cacherolesincookie = "True"   />
<! -- other attributes and default values:
cookiename = ". aspxroles "// cookie name
cookietimeout =" 30 "// cookie lifetime
cookiepath ="/"// Cookie Path
cookierequiressl =" false "// restrict cookie use of SSL
cookieslidingexpiration = "true" // re-apply for a full cookie
cookieprotection = "all"/> // cookie protection level
-->
</ System. Web >
</ Configuration >

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.