views:

3106

answers:

5

I am using aspx and c# for a setting a authentication cookie for a login. FormsAuthentication.SetAuthCookie(UserName, True)

I want to store more information in the same cookie. Can I add values to this authentication cookie or do I have to use a second http cookie?

Basically I'm looking for away to store the User's Id so I may be able to access the database using the users table row key

Thanks, Eden

A: 

You cannot change the value of the authentication cookie.

What if someone changes the value of that UserId cookie? Keep that information away from the user and store it on your server, maybe in the session object.

ZippyV
The authentication cookie is encrypted using a private <machinekey> by ASP.NET. The user can't change it without invalidating it.
Matt Spradley
I was talking about a separate userid cookie, which would not be automatically encrypted.
ZippyV
A: 

putting the userId in a cookie is not really a good idea, it can be changed by the client, save it in a session.

CD
+1  A: 

Pass that user ID as the userName param.

FormsAuthentication.SetAuthCookie(userId, True)

How are you securing your auth tickets?

I don't want to pass it _instead_ of the name. I would like to add more data to the same authentication cookie. is that possible at all?
Eden
+2  A: 

You can put whatever you want in the auth cookie as long as it's useful to you. That said, if you're putting sensitive information you should, at the very least, encrypt it, but I'd recommend against putting sensitive information there. You can do something like:

Forms.SetAuthCookie (UserName + "|" + UserId, true);

Then, whenever you need the username or the user id, it is there. Just load the cookie and parse out the values you need.

Again, I'd advise against doing this, especially as I have it presented above. That said, it is possible. You should create accessor methods to pull the data back out:

public int CurrentUserId
{
    get
    {
        int userId = 0;

        if (HttpContext.Current.Request.IsAuthenticated)
        {
            userId = Convert.ToInt32(HttpContext.Current.User.Identity.Name.Split('|')[1]);
        }

        return userId;
    }
}

public string CurrentUserName
{
    get
    {
        string userName = string.Empty;

        if (HttpContext.Current.Request.IsAuthenticated)
        {
            userName = HttpContext.Current.User.Identity.Name.Split('|')[0];
        }

        return userName;
    }
}
andymeadows
I should note that the way I have presented here is the "expedient" way and will work, but if I were to do it to have a smaller impact on the system as a whole I would use the approach Joe has presented.
andymeadows
+8  A: 

You can add user data to the FormsAuthenticationTicket, then generate the cookie yourself.

There's an example in the the MSDN documentation for FormsAuthenticationTicket.

Joe
Thanks! this exactly what I was looking for! It seems to overlap the behavior of FormsAuthentication.SetAuthCookie is asp.net 3.5 (I don't have the time to use reflector) with only one small difference. that you can attach arbitrary data to the cookie (not only the name). Thanks Again!
Eden