I am working with MVC 3 and I have just implemented a wrapper for the FormsAuthenticationService.
Something similar to the following.
public void SignIn(string username, bool createPersistantCookie)
{
if (string.IsNullOrEmpty(username))
throw new ArgumentException("Value Cannot be null or empty", "username");
FormsAuthentication.SetAuthCookie(username, createPersistantCookie);
}
Reluctantly, I have gotten this to work, but now I am not quite sure how to get the information that I have stored.
Once the user is in my system, how can I now safely retrieve this information if I need to grab their UserID out of the database?
Based on the additional information provided, you want to store additional data with the FormsAuthentication ticket. To do so, you need first create a custom FormsAuthentication ticket:
Storing Data
Grab the current HttpContext (not worrying about testability)
Determine when the ticket should expire:
Create a new FormsAuthentication ticket to hold your custom data.
Create your HTTP cookie
And finally add to the response
Retrieving Data
Then you can retrieve your data on subsequent requests by parsing the stored authentication ticket…
Again, grab current HttpContext
Check to see if the request has been authenticated (call in Application_AuthenticateRequest or OnAuthorize)
Check to see if you have a FormsAuthentication ticket available and that it has not expired:
Retrieve the FormsAuthentication ticket:
And finally retrieve your data: