For a web application I switched from using ASP.NET Membership to using my own log in system which just does something like this to mark a user as logged in:
Session["UserId"] = User.Id
Is it possible to store the user id in the ASPXAUTH cookie, piggybacking on its encryption, instead of using the standard session?
The goal is for the logged in state to last longer than a session and survive both browser and server restarts.
Update: The original answer provided was with a project using MembershipProvider and it’s explained in the answer itself. I, the asker, am not using it, so the answer to my problem was slightly different but extracted from this answer. I’m putting my answer at the bottom for anyone that cares and leaving the original verbatim, as it contains a lot of value.
Yes, you can use FormsAuthentication for your own strategy. And while the asp.net db structure does not suit you, you may provide a simple implementation of MembershipProvider to allow use of the Membership infrastructure. These two functionalities are not married so you may decide what fits for you.
Keeping in mind your question and some of the comments, here is a runnable example of how simple it is to leverage the provider model without being married to the default implementations and db schemas.
Using forms auth for your own purposes is simple. You just need to provide authentication and set your own ticket (cookie).
Using custom membership is almost as simple. You can implement as little or as much of the provider as you need to support the asp.net infrastructure features that you would like to employ.
e.g. in the sample below, I show that in the login process you may simply handle an event on the login control to validate credentials and the set the ticket. Done.
But I will also show how leveraging the provider model and implementing a custom membership provider can result in stronger, cleaner code. While we are in the custom membership provider I implement the minimum necessary to support using the Membership subsystem to provide easy access to a user’s meta data without the need to write your own infrastructure.
Just drop these files into an empty project.
web.config
Site1.Master
Login.aspx
Default.aspx
CustomAuthClasses.cs
Solution actually used (in as ASP.NET MVC project using OpenID):
I have an AccountController which I use to log users in and out and these methods are there.