J
Jesper Stocholm
I have implemented role-based security within my ASP.Net application.
However, it seems the role is not passed to the authentication ticket I
create.
I want to use it to display/hide some content based on the user's role. I
wrote this to do it:
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
plLoggedIn.Visible = true;
liFirstName.Text = HttpContext.Current.User.Identity.Name;
// This condition is causing me problems.
// The condition always returns false, and hence writes
// "user" regardless of what I log on as.
if (HttpContext.Current.User.IsInRole("Administrator"))
{
liUserRole.Text = "administrator";
}
else
{
liUserRole.Text = "user";
}
}
else
{
plLogin.Visible = true; // if not logged in, show login-form
}
I create my ticket as:
FormsAuthenticationTicket oTicket = new FormsAuthenticationTicket(
1,
txtUserName.Text, //user name from form
DateTime.Now,
DateTime.Now.AddMinutes(30),
false, //deletes cookie when closing browser session.
oData.GetString(0), //Data from db with value either "Administrator"
//or "User"
FormsAuthentication.FormsCookiePath
);
In my global.asax I added the code:
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id =
(FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
// Get the stored user-data, in this case, the
string userData = ticket.UserData; //Should contain e.g. "User"
string[] roles = userData.Split(',');
HttpContext.Current.User = new GenericPrincipal(id, roles);
}
}
}
It seems the ticket is created well enough - at least it is possible to
extract the username with User.Identity.Name, but the role passed as
userData above seems to be empty.
Is there any way to see what the role of a current user is - without
doing a explicit match like
User.IsInRole("<some role name>")
I would like to be able to do something similar to
someLabel.Text = "Your role is: " + User.Identity.Role();
.... but I cannot find the right way to do it.
I know this is a lot of code, but can any of you see where I am missing
something?
Thanks,
However, it seems the role is not passed to the authentication ticket I
create.
I want to use it to display/hide some content based on the user's role. I
wrote this to do it:
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
plLoggedIn.Visible = true;
liFirstName.Text = HttpContext.Current.User.Identity.Name;
// This condition is causing me problems.
// The condition always returns false, and hence writes
// "user" regardless of what I log on as.
if (HttpContext.Current.User.IsInRole("Administrator"))
{
liUserRole.Text = "administrator";
}
else
{
liUserRole.Text = "user";
}
}
else
{
plLogin.Visible = true; // if not logged in, show login-form
}
I create my ticket as:
FormsAuthenticationTicket oTicket = new FormsAuthenticationTicket(
1,
txtUserName.Text, //user name from form
DateTime.Now,
DateTime.Now.AddMinutes(30),
false, //deletes cookie when closing browser session.
oData.GetString(0), //Data from db with value either "Administrator"
//or "User"
FormsAuthentication.FormsCookiePath
);
In my global.asax I added the code:
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id =
(FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
// Get the stored user-data, in this case, the
string userData = ticket.UserData; //Should contain e.g. "User"
string[] roles = userData.Split(',');
HttpContext.Current.User = new GenericPrincipal(id, roles);
}
}
}
It seems the ticket is created well enough - at least it is possible to
extract the username with User.Identity.Name, but the role passed as
userData above seems to be empty.
Is there any way to see what the role of a current user is - without
doing a explicit match like
User.IsInRole("<some role name>")
I would like to be able to do something similar to
someLabel.Text = "Your role is: " + User.Identity.Role();
.... but I cannot find the right way to do it.
I know this is a lot of code, but can any of you see where I am missing
something?
Thanks,