Context.User problem

Discussion in 'ASP .Net' started by tshad, Apr 28, 2006.

  1. tshad

    tshad Guest

    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
     
    tshad, Apr 28, 2006
    #1
    1. Advertising

  2. You need to put the roles into a cookie.

    > 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
    >
     
    Shaun McDonnell, Apr 28, 2006
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:eBO$...
    >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
    >
     
    bruce barker \(sqlwork.com\), Apr 28, 2006
    #3
  4. tshad

    tshad Guest

    "Shaun McDonnell" <> wrote in message
    news:...
    > 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
    >>

    >
    >
     
    tshad, Apr 28, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VS_NET_DEV
    Replies:
    2
    Views:
    3,804
    jenny
    May 25, 2004
  2. =?Utf-8?B?U3VuU21pbGU=?=
    Replies:
    0
    Views:
    708
    =?Utf-8?B?U3VuU21pbGU=?=
    Jan 10, 2006
  3. Flip Rayner
    Replies:
    1
    Views:
    663
    bruce barker
    Jan 23, 2007
  4. asd
    Replies:
    1
    Views:
    439
    www.pulpjava.com
    Nov 9, 2006
  5. Jani Tiainen
    Replies:
    3
    Views:
    1,022
    Jani Tiainen
    Aug 27, 2007
Loading...

Share This Page