FormsAuthentication after login

Ok, I have a simple script:

have two pages: login and welcome pages. im using FormsAuthentication with my own table which has four columns: ID, UserName, Password, FullName

On clicking login im setting my username:

FormsAuthentication.SetAuthCookie(userName, rememberMe ?? false);

      

on the welcome page that I cannot use:

Page.User.Identity.Name

      

to provide the user who is currently logged in, BUT i dont user username as in all examples in http://asp.net website i want user field FullName

I think I always go to the db and ask for the full name when the page loads its crazy and doesn't like user sessions or Simple Cookie. FormsAuth provider has custom fields for this

0


source to share


6 answers


I would store the full username in the session cookie after your FormsAuth call

FormsAuth.SetAuthCookie(userName, rememberme);

// get the full name (ex "John Doe") from the datbase here during login
string fullName = "John Doe";

Response.Cookies["FullName"].Value = fullName;
Response.Cookies["FullName"].expires = DateTime.Now.AddDays(30);

      



and then extract it in your view pages via:

string fullName = HttpContext.Current.Request.Cookies["FullName"].Value

      

+1


source


Forms are authenticated using cookies. You can create your own cookie and put the full name in it, but I think I'll send it to the session. If you are using any cookie you will need to fetch the name from it every time. Linking it to a session feels more natural and makes it easier for you to access. I agree that it seems like a waste to go back to the DB every time, and I would of course cache the value somewhere.



Information on creating your own forms authentication cookie can be found here .

+1


source


Sorry I'm a little late to the party, but here's how you can do it without storing the value elsewhere :)

var authCookieKey = FormsAuthentication.FormsCookieName;
var responseCookies = HttpContext.Current.Response.Cookies;
var requestCookies = HttpContext.Current.Request.Cookies;

var aspxAuthCookieInResponse = responseCookies.AllKeys.Contains(authCookieKey) ? responseCookies[authCookieKey] : null;
var aspxAuthCookieInRequest = requestCookies.AllKeys.Contains(authCookieKey) ? requestCookies[authCookieKey] : null;

// Take ASPXAUTH cookie from either response or request.
var cookie = aspxAuthCookieInResponse ?? aspxAuthCookieInRequest;
var authTicket = FormsAuthentication.Decrypt(cookie.Value); // Todo: Check for nulls.

// Using the name!
var userName = authTicket.Name;

      

+1


source


There are no custom fields for form authentication. You will just need to use a session. That's what he knows there for you.;) Don't forget that cookies and authentication sessions are two independent things. They even have their own timeouts. This way the session will not be reset when the user logs out unless you do it yourself.

0


source


How do I use profiles to store additional information with the user?

0


source


The easiest option is to use a session. By default, session state is retained in memory and will be lost when the ASP.NET worker process restarts, however, you can configure it instead of using a state service that stores session information in a separate process:

http://msdn.microsoft.com/en-us/library/ms178586.aspx

Another option is to use profiles. It looks like you already have the profile information stored in your own tables, you probably have to write a custom provider for it to make it a more complex solution.

0


source







All Articles