Role authentication does not work in asp.net

I use the code below to access a page database based on user authentication

if (user.FirstOrDefault() == HashedPassword)
{
    string roles = "Member";

    // Create the authentication ticket
    FormsAuthenticationTicket authTicket = new
        FormsAuthenticationTicket(1,                          //  version
                                  loginName.Text,             // user name
                                  DateTime.Now,               //  creation 
                                  DateTime.Now.AddMinutes(60),// Expiration
                                  false,                      //  Persistent
                                  roles);                     // User data

    // Now encrypt the ticket.
    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
    // Create a cookie and add the encrypted ticket to the
    // cookie as data.
    HttpCookie authCookie = 
                new HttpCookie(FormsAuthentication.FormsCookieName,
                               encryptedTicket);
    // Add the cookie to the outgoing cookies collection.
    Response.Cookies.Add(authCookie);

    Response.Redirect("/Members/ClientAccount.aspx");    
}
else
{
    Response.Redirect("signin.aspx");
}

}

The user gets access to ClientAccount.aspx if the login information is correct, but I want this to happen only if his / her role is set as Admin, as shown in the web.config file below.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <location path="members.aspx">
        <system.web>
            <authorization>
                <allow roles="Member" />
                <allow roles="Admin" />
                <deny users="?" />
            </authorization>
        </system.web>
    </location>
    <location path="ClientAccount.aspx">
        <system.web>
            <authorization>                    
                <allow roles="Admin" />
                <deny roles="Member"/>
                <deny users="?" />
            </authorization>
        </system.web>
    </location>
</configuration>

How to do it?

I think the web.config file does not look at the cookie to make authorization, so I am doing something wrong.

+3
source share
2 answers

Double check your location path relative to web.config, I think this is a problem.

<location path="/Members/ClientAccount.aspx">
    ...
</location>

Of course, you need to do something different instead of this line, you just did it for testing, I would suggest?

 Response.Redirect("/Members/ClientAccount.aspx");    

. , , , . , , , , .

, web.config :

<authentication mode="Forms" />

, :

<authentication mode="Forms">
    <forms loginUrl="Login.aspx"
           protection="All"
           timeout="30"
           name=".ASPXAUTH" 
           path="/"
           requireSSL="false"
           slidingExpiration="true"
           defaultUrl="default.aspx"
           cookieless="UseDeviceProfile"
           enableCrossAppRedirects="false" />
</authentication>

http://msdn.microsoft.com/en-us/library/ff647070.aspx

+2

,

< deny role = "Member" /" >

, deny . , , deny, :

<authorization>
  <allow roles="Admin" />
  <allow roles="Member"/>
  <deny users="?" />
</authorization>
0

All Articles