programmatic login with .net membership provider

ddc0660 picture ddc0660 · Oct 28, 2008 · Viewed 11.9k times · Source

I'm trying to unit test a piece of code that needs a currently logged in user in the test. Using the .Net 2.0 Membership Provider, how can I programmatically log in as a user for this test?

Answer

ronald picture ronald · Jul 10, 2009
if(Membership.ValidateUser("user1",P@ssw0rd))
        {
            FormsAuthentication.SetAuthCookie("user1",true); 
}