Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to work with role management in asp.net.
Posted

Role-based Security with Forms Authentication[^] article from CP might help you.
 
Share this answer
 
Comments
Dalek Dave 28-Mar-11 11:41am    
Good Link
[no name] 28-Mar-11 23:45pm    
Thanks Dalek
Add this code to your Web.Config File :

XML
<configuration>
  <system.web>
    <authorization>
      <allow roles="Manager" />
      <deny users=""*"" />
    </authorization>
  </system.web>
</configuration>


Now you have to map the roles stored in the database to user accounts in each and every request so ASP.NET can determine whether the requestor is a manager or other( EX: S/w developer).


I hope this will help you .
 
Share this answer
 
Comments
Dalek Dave 28-Mar-11 11:42am    
Good Answer
 
Share this answer
 
Comments
Dalek Dave 28-Mar-11 11:42am    
Good Link
Form authentication works like this:

XML
<location path="secure">
    <system.web>
      <authorization>
        <deny users="?"/>
        <deny users="jhon"/>
      </authorization>
    </system.web>
  </location>

secure is a folder which contains your secure webforms.

XML
<authentication mode="Forms">
      <forms loginUrl="Default.aspx"
           protection="All"
           timeout="30"
           name=".ASPXAUTH"
           path="/"
           requireSSL="false"
           slidingExpiration="true"
           defaultUrl="default.aspx"
           cookieless="UseDeviceProfile"
           enableCrossAppRedirects="false" >
        <credentials passwordFormat="Clear">
          <user name="kim" password="kim@123"/>
          <user name="jhon" password="jhonn"/>
        </credentials>
      </forms>
    </authentication>


Now at server side code
Default.aspx is your login form, Drag Two TextBoxes and a Button
at click event of button write following code. Default2.aspx is destination page. Secure is a folder which can have webforms which you wants to make secure


if (FormsAuthentication.Authenticate(TextBox1.Text, TextBox2.Text))
        {
            FormsAuthentication.SetAuthCookie(
                 this.TextBox1.Text.Trim(), false);
            FormsAuthenticationTicket ticket1 =
               new FormsAuthenticationTicket(
                    1,                                   // version
                    this.TextBox1.Text.Trim(),   // get username  from the form
                    DateTime.Now,                        // issue time is now
                    DateTime.Now.AddMinutes(10),         // expires in 10 minutes
                    false,      // cookie is not persistent
                    "HR"                              // role assignment is stored
                // in userData
                    );
            HttpCookie cookie1 = new HttpCookie(
              FormsAuthentication.FormsCookieName,
              FormsAuthentication.Encrypt(ticket1));
            Response.Cookies.Add(cookie1);
            // 4. Do the redirect. 
            String returnUrl1;
            // the login is successful
            if (Request.QueryString["ReturnUrl"] == null)
            {
                returnUrl1 = "Default2.aspx";
            }
            //login not unsuccessful 
            else
            {
                returnUrl1 = Request.QueryString["ReturnUrl"];
            }
            Response.Redirect(returnUrl1);
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900