Re: Context.User problem

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



context is valid for a single request (page render). when you redirect to a
new page, that page gets a new context. you need to store your
authentication info somewhere that the client will send to you on each
request. (say a cookie or url munging)

-- bruce (sqlwork.com)


"tshad" <tscheiderich@xxxxxxxxxxxxxxx> wrote in message
news:eBO$D1uaGHA.504@xxxxxxxxxxxxxxxxxxxxxxx
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



.



Relevant Pages