Re: HOW TO: Setting Up Forms Authentication
From: John Saunders (john.saunders_at_surfcontrol.com)
Date: 08/07/03
- Next message: VB Programmer: "HOW TO: Setting Up Forms Authentication (Revised)"
- Previous message: VB Programmer: "Re: HOW TO: Setting Up Forms Authentication"
- In reply to: VB Programmer: "Re: HOW TO: Setting Up Forms Authentication"
- Next in thread: VB Programmer: "HOW TO: Setting Up Forms Authentication (Revised)"
- Reply: VB Programmer: "HOW TO: Setting Up Forms Authentication (Revised)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Thu, 7 Aug 2003 11:58:18 -0400
"VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
news:uAMxYlPXDHA.1896@TK2MSFTNGP12.phx.gbl...
> Cool. That's basically what I did.
>
> 1. What defines where the custom cookie is stored? I used to see the
> default cookie in "C:\Documents and Settings\Administrator\Cookies", but
now
> I can't find my custom cookie?
If you don't set an expiration date on a cookie, it will be a "session
cookie", which I don't believe is stored on disk. Session cookies are a Good
Thing, as browsers are more likely to be set to accept them than permanent
cookies.
> 2. How do I retrieve the roles that are stored in UserData (ticket)?
By doing the opposite of of what you did to put them there. :-)
For instance, if your database code in login produced an array of roles, you
might use:
string[] roles = GetRolesForUser(userName);
string userData = String.Join(",", roles);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
userName,
System.DateTime.Now,
System.DateTime.Now.AddMinutes(30),
isPersistent,
userData,
FormsAuthentication.FormsCookiePath);
// Encrypt the ticket.
string encTicket = FormsAuthentication.Encrypt(ticket);
// Create the cookie.
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName,
encTicket));
// Redirect back to original URL.
Response.Redirect(FormsAuthentication.GetRedirectUrl(userName,isPersistent))
;
Well, in this case you'll want to do the following in
Application_AuthenticateRequest:
FormsIdentity fi = User.Identity as FormsIdentity;
if (fi == null) return; // don't know how _that_ happened!
FormsAuthenticationTicket ticket = fi.Ticket;
string userData = ticket.UserData;
string roles[] = userData.Split(',');
Request.User = new GenericPrincipal(fi, roles);
> 3. What is a common reason why you would access this in
> Application_AuthenticateRequest? This seems to work with no code in
> Application_AuthenticateRequest.
But it's not working. If you put the user in a role right now, is he still
in the same role on all subsequent requests? I doubt it. You need to set the
Principal on each request - remember we're talking "stateless".
-- John Saunders Internet Engineer john.saunders@surfcontrol.com > You're a great resource! Thanks. You're welcome. -- John Saunders Internet Engineer john.saunders@surfcontrol.com
- Next message: VB Programmer: "HOW TO: Setting Up Forms Authentication (Revised)"
- Previous message: VB Programmer: "Re: HOW TO: Setting Up Forms Authentication"
- In reply to: VB Programmer: "Re: HOW TO: Setting Up Forms Authentication"
- Next in thread: VB Programmer: "HOW TO: Setting Up Forms Authentication (Revised)"
- Reply: VB Programmer: "HOW TO: Setting Up Forms Authentication (Revised)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|