I use the code below to access a page database based on user authentication
if (user.FirstOrDefault() == HashedPassword)
{
string roles = "Member";
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1,
loginName.Text,
DateTime.Now,
DateTime.Now.AddMinutes(60),
false,
roles);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie =
new HttpCookie(FormsAuthentication.FormsCookieName,
encryptedTicket);
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.
source
share