Forms Authentication - Really really basic question

Discussion in 'ASP .Net' started by =?Utf-8?B?TFc=?=, Apr 24, 2007.

  1. Hello!
    I am just learning about forms authentication so please excuse this basic
    question. I am using .NET 1.1 and C#.

    I have created my web.config file and my login.aspx and the associated cs
    file using
    examples on MSDN. I have created a FormsAuthenticationTicket and cookie and
    added the cookie to the response and then set the SetAuthCookie etc. When I
    go to the redirected page, I am not sure how to read the cookie value so I
    know who the user is!

    In my FirstPage.aspx.cs's Page Load method, I have

    string userID = Request.Cookies[FormsAuthentication.FormsCookieName].Value;

    which is blank. My question is, once I read have created the ticket and
    cookie, how to I read it ? Most examples I am looking at talk about the
    creating tickets and cookies in forms authentication and not how to read the
    cookie so any help will be greatly appreciated. Do I need to decrypt the
    ticket?cookie?

    Thanks,
    =?Utf-8?B?TFc=?=, Apr 24, 2007
    #1
    1. Advertising

  2. Hi,
    In below link it shows how to authenticate cookies in other pages.
    http://www.15seconds.com/Issue/020220.htm
    Search for User.Identity.IsAuthenticated in above link.
    Also To enable forms authentication, cookies must be enabled on the client
    browser. If the client disables cookies, the cookie generated by Forms
    Authentication is lost and the client will not be able to authenticate.

    Hope this helps
    --
    If my answer helped you,then please do press Yes below.
    Thanks and Regards.
    Manish Bafna.
    MCP and MCTS.



    "LW" wrote:

    > Hello!
    > I am just learning about forms authentication so please excuse this basic
    > question. I am using .NET 1.1 and C#.
    >
    > I have created my web.config file and my login.aspx and the associated cs
    > file using
    > examples on MSDN. I have created a FormsAuthenticationTicket and cookie and
    > added the cookie to the response and then set the SetAuthCookie etc. When I
    > go to the redirected page, I am not sure how to read the cookie value so I
    > know who the user is!
    >
    > In my FirstPage.aspx.cs's Page Load method, I have
    >
    > string userID = Request.Cookies[FormsAuthentication.FormsCookieName].Value;
    >
    > which is blank. My question is, once I read have created the ticket and
    > cookie, how to I read it ? Most examples I am looking at talk about the
    > creating tickets and cookies in forms authentication and not how to read the
    > cookie so any help will be greatly appreciated. Do I need to decrypt the
    > ticket?cookie?
    >
    > Thanks,
    =?Utf-8?B?TWFuaXNoIEJhZm5h?=, Apr 24, 2007
    #2
    1. Advertising

  3. If the auth cookie is coming back from the browser, you should be getting the
    cookie with your code:
    Request.Cookies[FormsAuthentication.FormsCookieName].Value. Not sure if the
    auth cookie is really sent to the browser (thru Response.Redirect (target
    page after authentication) in your case.

    Anyway, Context.User.Identity.Name gives the currently logged on user's name
    (including Forms Auth mode). However, this works only if the auth cookie was
    received from the browser.

    "LW" wrote:

    > Hello!
    > I am just learning about forms authentication so please excuse this basic
    > question. I am using .NET 1.1 and C#.
    >
    > I have created my web.config file and my login.aspx and the associated cs
    > file using
    > examples on MSDN. I have created a FormsAuthenticationTicket and cookie and
    > added the cookie to the response and then set the SetAuthCookie etc. When I
    > go to the redirected page, I am not sure how to read the cookie value so I
    > know who the user is!
    >
    > In my FirstPage.aspx.cs's Page Load method, I have
    >
    > string userID = Request.Cookies[FormsAuthentication.FormsCookieName].Value;
    >
    > which is blank. My question is, once I read have created the ticket and
    > cookie, how to I read it ? Most examples I am looking at talk about the
    > creating tickets and cookies in forms authentication and not how to read the
    > cookie so any help will be greatly appreciated. Do I need to decrypt the
    > ticket?cookie?
    >
    > Thanks,
    =?Utf-8?B?U2l2YSBN?=, Apr 24, 2007
    #3
  4. Thank you Siva and Manish. I am now getting the user name like so:
    Page.User.Identity.Name.

    Also, I printed the value for Page.User.Identity.IsAuthenticated (True)
    and Page.User.Identity.AuthenticaionType (Forms), so I am guessing that I
    am using forms authentication and the cookie is being sent to the browser.

    What you are saying is that
    Request.Cookies[FormsAuthentication.FormsCookieName].Value should also have
    the value of the user name, right ? I will recheck my code to see why that
    is blank.

    Thanks,
    LW

    "Siva M" wrote:

    > If the auth cookie is coming back from the browser, you should be getting the
    > cookie with your code:
    > Request.Cookies[FormsAuthentication.FormsCookieName].Value. Not sure if the
    > auth cookie is really sent to the browser (thru Response.Redirect (target
    > page after authentication) in your case.
    >
    > Anyway, Context.User.Identity.Name gives the currently logged on user's name
    > (including Forms Auth mode). However, this works only if the auth cookie was
    > received from the browser.
    >
    > "LW" wrote:
    >
    > > Hello!
    > > I am just learning about forms authentication so please excuse this basic
    > > question. I am using .NET 1.1 and C#.
    > >
    > > I have created my web.config file and my login.aspx and the associated cs
    > > file using
    > > examples on MSDN. I have created a FormsAuthenticationTicket and cookie and
    > > added the cookie to the response and then set the SetAuthCookie etc. When I
    > > go to the redirected page, I am not sure how to read the cookie value so I
    > > know who the user is!
    > >
    > > In my FirstPage.aspx.cs's Page Load method, I have
    > >
    > > string userID = Request.Cookies[FormsAuthentication.FormsCookieName].Value;
    > >
    > > which is blank. My question is, once I read have created the ticket and
    > > cookie, how to I read it ? Most examples I am looking at talk about the
    > > creating tickets and cookies in forms authentication and not how to read the
    > > cookie so any help will be greatly appreciated. Do I need to decrypt the
    > > ticket?cookie?
    > >
    > > Thanks,
    =?Utf-8?B?TFc=?=, Apr 24, 2007
    #4
  5. Hi,
    just a gentle reminder.If you have found our post useful then please do
    press Yes below
    --
    If my answer helped you,then please do press Yes below.
    Thanks and Regards.
    Manish Bafna.
    MCP and MCTS.



    "LW" wrote:

    > Hello!
    > I am just learning about forms authentication so please excuse this basic
    > question. I am using .NET 1.1 and C#.
    >
    > I have created my web.config file and my login.aspx and the associated cs
    > file using
    > examples on MSDN. I have created a FormsAuthenticationTicket and cookie and
    > added the cookie to the response and then set the SetAuthCookie etc. When I
    > go to the redirected page, I am not sure how to read the cookie value so I
    > know who the user is!
    >
    > In my FirstPage.aspx.cs's Page Load method, I have
    >
    > string userID = Request.Cookies[FormsAuthentication.FormsCookieName].Value;
    >
    > which is blank. My question is, once I read have created the ticket and
    > cookie, how to I read it ? Most examples I am looking at talk about the
    > creating tickets and cookies in forms authentication and not how to read the
    > cookie so any help will be greatly appreciated. Do I need to decrypt the
    > ticket?cookie?
    >
    > Thanks,
    =?Utf-8?B?TWFuaXNoIEJhZm5h?=, Apr 24, 2007
    #5
  6. LW,

    The cookie will NOT have the actual user name, but the encrypted
    FormsAuthenticationTicket instance represnting the current user name and
    additional data. To get the user name from the cookie value:

    FormsAuthentication.Decrypt
    (Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name

    "LW" wrote:

    > Thank you Siva and Manish. I am now getting the user name like so:
    > Page.User.Identity.Name.
    >
    > Also, I printed the value for Page.User.Identity.IsAuthenticated (True)
    > and Page.User.Identity.AuthenticaionType (Forms), so I am guessing that I
    > am using forms authentication and the cookie is being sent to the browser.
    >
    > What you are saying is that
    > Request.Cookies[FormsAuthentication.FormsCookieName].Value should also have
    > the value of the user name, right ? I will recheck my code to see why that
    > is blank.
    >
    > Thanks,
    > LW
    >
    > "Siva M" wrote:
    >
    > > If the auth cookie is coming back from the browser, you should be getting the
    > > cookie with your code:
    > > Request.Cookies[FormsAuthentication.FormsCookieName].Value. Not sure if the
    > > auth cookie is really sent to the browser (thru Response.Redirect (target
    > > page after authentication) in your case.
    > >
    > > Anyway, Context.User.Identity.Name gives the currently logged on user's name
    > > (including Forms Auth mode). However, this works only if the auth cookie was
    > > received from the browser.
    > >
    > > "LW" wrote:
    > >
    > > > Hello!
    > > > I am just learning about forms authentication so please excuse this basic
    > > > question. I am using .NET 1.1 and C#.
    > > >
    > > > I have created my web.config file and my login.aspx and the associated cs
    > > > file using
    > > > examples on MSDN. I have created a FormsAuthenticationTicket and cookie and
    > > > added the cookie to the response and then set the SetAuthCookie etc. When I
    > > > go to the redirected page, I am not sure how to read the cookie value so I
    > > > know who the user is!
    > > >
    > > > In my FirstPage.aspx.cs's Page Load method, I have
    > > >
    > > > string userID = Request.Cookies[FormsAuthentication.FormsCookieName].Value;
    > > >
    > > > which is blank. My question is, once I read have created the ticket and
    > > > cookie, how to I read it ? Most examples I am looking at talk about the
    > > > creating tickets and cookies in forms authentication and not how to read the
    > > > cookie so any help will be greatly appreciated. Do I need to decrypt the
    > > > ticket?cookie?
    > > >
    > > > Thanks,
    =?Utf-8?B?U2l2YSBN?=, Apr 24, 2007
    #6
  7. Please don't spam this newsgroup with your requests for votes.

    Many people can't even see the Yes button, because they're using
    newsreaders, not browsers, to ask questions and reply to them.




    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ===================================
    "Manish Bafna" <> wrote in message
    news:...
    > Hi,
    > just a gentle reminder.If you have found our post useful then please do
    > press Yes below
    > --
    > If my answer helped you,then please do press Yes below.
    > Thanks and Regards.
    > Manish Bafna.
    > MCP and MCTS.
    >
    >
    >
    > "LW" wrote:
    >
    >> Hello!
    >> I am just learning about forms authentication so please excuse this basic
    >> question. I am using .NET 1.1 and C#.
    >>
    >> I have created my web.config file and my login.aspx and the associated cs
    >> file using
    >> examples on MSDN. I have created a FormsAuthenticationTicket and cookie and
    >> added the cookie to the response and then set the SetAuthCookie etc. When I
    >> go to the redirected page, I am not sure how to read the cookie value so I
    >> know who the user is!
    >>
    >> In my FirstPage.aspx.cs's Page Load method, I have
    >>
    >> string userID = Request.Cookies[FormsAuthentication.FormsCookieName].Value;
    >>
    >> which is blank. My question is, once I read have created the ticket and
    >> cookie, how to I read it ? Most examples I am looking at talk about the
    >> creating tickets and cookies in forms authentication and not how to read the
    >> cookie so any help will be greatly appreciated. Do I need to decrypt the
    >> ticket?cookie?
    >>
    >> Thanks,
    Juan T. Llibre, Apr 24, 2007
    #7
  8. I just voted for you!
    :)
    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short urls & more: http://ittyurl.net




    "Juan T. Llibre" wrote:

    > Please don't spam this newsgroup with your requests for votes.
    >
    > Many people can't even see the Yes button, because they're using
    > newsreaders, not browsers, to ask questions and reply to them.
    >
    >
    >
    >
    > Juan T. Llibre, asp.net MVP
    > asp.net faq : http://asp.net.do/faq/
    > foros de asp.net, en español : http://asp.net.do/foros/
    > ===================================
    > "Manish Bafna" <> wrote in message
    > news:...
    > > Hi,
    > > just a gentle reminder.If you have found our post useful then please do
    > > press Yes below
    > > --
    > > If my answer helped you,then please do press Yes below.
    > > Thanks and Regards.
    > > Manish Bafna.
    > > MCP and MCTS.
    > >
    > >
    > >
    > > "LW" wrote:
    > >
    > >> Hello!
    > >> I am just learning about forms authentication so please excuse this basic
    > >> question. I am using .NET 1.1 and C#.
    > >>
    > >> I have created my web.config file and my login.aspx and the associated cs
    > >> file using
    > >> examples on MSDN. I have created a FormsAuthenticationTicket and cookie and
    > >> added the cookie to the response and then set the SetAuthCookie etc. When I
    > >> go to the redirected page, I am not sure how to read the cookie value so I
    > >> know who the user is!
    > >>
    > >> In my FirstPage.aspx.cs's Page Load method, I have
    > >>
    > >> string userID = Request.Cookies[FormsAuthentication.FormsCookieName].Value;
    > >>
    > >> which is blank. My question is, once I read have created the ticket and
    > >> cookie, how to I read it ? Most examples I am looking at talk about the
    > >> creating tickets and cookies in forms authentication and not how to read the
    > >> cookie so any help will be greatly appreciated. Do I need to decrypt the
    > >> ticket?cookie?
    > >>
    > >> Thanks,

    >
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Apr 24, 2007
    #8
  9. Siva/Manish,

    This is exactly what I was looking for:

    FormsAuthentication.Decrypt
    (Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name

    Thank you very much for your helpful posts!
    LW

    "Siva M" wrote:

    > LW,
    >
    > The cookie will NOT have the actual user name, but the encrypted
    > FormsAuthenticationTicket instance represnting the current user name and
    > additional data. To get the user name from the cookie value:
    >
    > FormsAuthentication.Decrypt
    > (Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name
    >
    > "LW" wrote:
    >
    > > Thank you Siva and Manish. I am now getting the user name like so:
    > > Page.User.Identity.Name.
    > >
    > > Also, I printed the value for Page.User.Identity.IsAuthenticated (True)
    > > and Page.User.Identity.AuthenticaionType (Forms), so I am guessing that I
    > > am using forms authentication and the cookie is being sent to the browser.
    > >
    > > What you are saying is that
    > > Request.Cookies[FormsAuthentication.FormsCookieName].Value should also have
    > > the value of the user name, right ? I will recheck my code to see why that
    > > is blank.
    > >
    > > Thanks,
    > > LW
    > >
    > > "Siva M" wrote:
    > >
    > > > If the auth cookie is coming back from the browser, you should be getting the
    > > > cookie with your code:
    > > > Request.Cookies[FormsAuthentication.FormsCookieName].Value. Not sure if the
    > > > auth cookie is really sent to the browser (thru Response.Redirect (target
    > > > page after authentication) in your case.
    > > >
    > > > Anyway, Context.User.Identity.Name gives the currently logged on user's name
    > > > (including Forms Auth mode). However, this works only if the auth cookie was
    > > > received from the browser.
    > > >
    > > > "LW" wrote:
    > > >
    > > > > Hello!
    > > > > I am just learning about forms authentication so please excuse this basic
    > > > > question. I am using .NET 1.1 and C#.
    > > > >
    > > > > I have created my web.config file and my login.aspx and the associated cs
    > > > > file using
    > > > > examples on MSDN. I have created a FormsAuthenticationTicket and cookie and
    > > > > added the cookie to the response and then set the SetAuthCookie etc. When I
    > > > > go to the redirected page, I am not sure how to read the cookie value so I
    > > > > know who the user is!
    > > > >
    > > > > In my FirstPage.aspx.cs's Page Load method, I have
    > > > >
    > > > > string userID = Request.Cookies[FormsAuthentication.FormsCookieName].Value;
    > > > >
    > > > > which is blank. My question is, once I read have created the ticket and
    > > > > cookie, how to I read it ? Most examples I am looking at talk about the
    > > > > creating tickets and cookies in forms authentication and not how to read the
    > > > > cookie so any help will be greatly appreciated. Do I need to decrypt the
    > > > > ticket?cookie?
    > > > >
    > > > > Thanks,
    =?Utf-8?B?TFc=?=, Apr 25, 2007
    #9
    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. Brett Porter
    Replies:
    2
    Views:
    756
    Andrea D'Onofrio [MSFT]
    Jan 20, 2004
  2. Brett Porter
    Replies:
    5
    Views:
    583
    Brett Porter
    Feb 3, 2004
  3. Eric
    Replies:
    2
    Views:
    1,446
    Tommy
    Feb 13, 2004
  4. Brett Porter
    Replies:
    2
    Views:
    194
    Andrea D'Onofrio [MSFT]
    Jan 20, 2004
  5. LW
    Replies:
    1
    Views:
    173
    Alexey Smirnov
    May 2, 2007
Loading...

Share This Page