T
tshad
I am playing with GenericPrincipal classes and am using a sample program to
test it.
The problem is that even though I set the roles (which shows the roles in
the Context.User as being there), when the program goes from the login page
to the next page - the roles in the Context.User is empty.
My Login page is:
*****************************************************************
private void btnAuthenticate_Click(object sender, System.EventArgs e)
{
if(txtUserName.Text == "Adam" &&
txtPassword.Text == "Dinosaur")
{
string[] roles = {"Admin", "User"};
DoAuthenticate(txtUserName.Text, roles);
}
if(Context.User.Identity.IsAuthenticated )
{
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text,
false);
}
}
private void DoAuthenticate (string userName, string[] roles)
{
GenericIdentity userIdentity = new GenericIdentity(userName);
GenericPrincipal userPrincipal =
new GenericPrincipal(userIdentity, roles);
Context.User = userPrincipal;
}
**************************************************************
At this point, the roles in the Context.User has a string array containing
"Admin" and "User".
The Redirect goes to my default.aspx page which is:
************************************************************
void Page_Load(object sender, EventArgs e)
{
if(User.Identity.IsAuthenticated )
{
if(Context.User.IsInRole("Admin"))
{
lblIdentity.Text += " (Admin)";
}
lblIdentity.Text = "The current user is " + User.Identity.Name;
}
else
{
if(Context.User.IsInRole("Admin"))
{
lblIdentity.Text += " (Admin)";
}
lblIdentity.Text = "The current user is not authenticated.";
}
}
************************************************************
When I go to this page roles in my Context.User is empty.
Why is that?
Thanks,
Tom
test it.
The problem is that even though I set the roles (which shows the roles in
the Context.User as being there), when the program goes from the login page
to the next page - the roles in the Context.User is empty.
My Login page is:
*****************************************************************
private void btnAuthenticate_Click(object sender, System.EventArgs e)
{
if(txtUserName.Text == "Adam" &&
txtPassword.Text == "Dinosaur")
{
string[] roles = {"Admin", "User"};
DoAuthenticate(txtUserName.Text, roles);
}
if(Context.User.Identity.IsAuthenticated )
{
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text,
false);
}
}
private void DoAuthenticate (string userName, string[] roles)
{
GenericIdentity userIdentity = new GenericIdentity(userName);
GenericPrincipal userPrincipal =
new GenericPrincipal(userIdentity, roles);
Context.User = userPrincipal;
}
**************************************************************
At this point, the roles in the Context.User has a string array containing
"Admin" and "User".
The Redirect goes to my default.aspx page which is:
************************************************************
void Page_Load(object sender, EventArgs e)
{
if(User.Identity.IsAuthenticated )
{
if(Context.User.IsInRole("Admin"))
{
lblIdentity.Text += " (Admin)";
}
lblIdentity.Text = "The current user is " + User.Identity.Name;
}
else
{
if(Context.User.IsInRole("Admin"))
{
lblIdentity.Text += " (Admin)";
}
lblIdentity.Text = "The current user is not authenticated.";
}
}
************************************************************
When I go to this page roles in my Context.User is empty.
Why is that?
Thanks,
Tom