asp.net create user and role groups, etc.

Source: Internet
Author: User


1. Webconfig Configuration

User basic information needs to be configured membership node, located under <system.web>


<membership defaultprovider= "SQLProvider" userisonlinetimewindow= ">
	<providers> <"
		Remove name= "AspNetSqlProvider"/>
		<add name= "SQLProvider"
            		 System.Web.Security.SqlMembershipProvider "
			 connectionstringname=" Sqlservices 
             		 " Requiresquestionandanswer= "false"
          		 requiresuniqueemail= "false"
           		 maxinvalidpasswordattempts= "   
                         Minrequiredpasswordlength= "1"   
                         minrequirednonalphanumericcharacters= "0"   
                         Passwordstrengthregularexpression= ""  
          		 applicationname= "/"
		/>
	</providers>
</ Membership>


Creating profile information for a user requires a Profiles node, located under <system.web>

<profile enabled= "true" defaultprovider= "SQLProvider" >
	<providers>
		<clear/>
		< Add Name= "SQLProvider"
			 type= "System.Web.Profile.SqlProfileProvider"
			 connectionstringname= "Sqlservices"
			 applicationname= "/"
			 description= "SqlProfileProvider for SampleApplication"/>
	</providers>
	<properties>
 		<add name= "style" type= "System.String" ></add>
		<add name= "age" type = "System.String"  ></add>
		<add name= "School" type= "System.String" ></add>
	< /properties>
</profile>



Creating a user's role information requires a rolemanager node, located under <system.web>

<rolemanager   
	enabled= "true" 
	defaultprovider= "SQLProvider"  
	cacherolesincookie= "true"      
	Cookiename= ". Vs2005_role "     
	cookietimeout=" "      
	cookiepath="/"      
	cookierequiressl=" false "      
	Cookieslidingexpiration= "true"      
	cookieprotection= "All" >
      <providers>
        <add	name= " SQLProvider "          
			type=" System.Web.Security.SqlRoleProvider "          
			connectionstringname=" Sqlservices         
			 " Applicationname= "/"/>
      </providers>
      
    </roleManager>



Configure ConnectionString, located under Configuration node

	<connectionStrings>
		<add name= "sqlservices" connectionstring= "server=servername;database=dbname; Uid=username;pwd=password "/>
	</connectionStrings>

Configure authentication mode, located under <system.web>

  <!--
            through the <authentication> section you can configure ASP.net to 
            identify the
            security authentication mode that enters the user. 
        -->
  <!--authentication Mode-->
  <authentication mode= "Forms" >
    <forms loginurl= "~/login.aspx" "></forms>
  </authentication>



2. Related code

      	 Adds a namespace
	using System.Web.Security;
	Create User UserName
	membership.createuser ("UserName", "Password");
	Add profile for new user
	ProfileCommon	p =  profile.getprofile ("UserName");
	P.school = "Shanghai Aquatic University";
	P.age = "n";
	P.style = "President of the Student Union";
	P.save ();
	Add a role group
	roles.createrole ("admin");
	Add new users to the admin group
	roles.addusertorole ("UserName", "admin");
	Verify that the user belongs to the Admin role group
	if (Roles.isuserinrole ("UserName", "admin")
	{
		//do something
	}





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.