Context.User problem

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
 
B

bruce barker \(sqlwork.com\)

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)
 
T

tshad

Shaun McDonnell said:
You need to put the roles into a cookie.

That was it.

Thanks,

Tom
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top