Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET forms authentication - auto login with a test account while debugging?

I have a web application that uses the asp.net membership and role providers to allow logins that are members of certain roles to have access to various pages depending on role assignments.

During debugging I'd like the app to log in automatically with a test account, so I can check the functionality of the role assignments, and not have to go through entering credentials on the login page each time. Is there an easy way to do this?

like image 983
CharlieG Avatar asked Jul 01 '09 20:07

CharlieG


2 Answers

Jeff is right, you can do it trough global.asax method:

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
   if(System.Diagnostics.Debugger.IsAttached && User == null)
   {
       FormsAuthentication.SetAuthCookie("dmike", false);
   }
}

cheers

like image 183
Marko Avatar answered Sep 27 '22 20:09

Marko


This code does the job. In Login.aspx's Page_Load event:

    Membership.ValidateUser("<userName>", "<password>")
    FormsAuthentication.RedirectFromLoginPage("<userName>", True)

MSDN Documentation

Note: Membership uses the System.Web.Security reference.

like image 30
CharlieG Avatar answered Sep 27 '22 19:09

CharlieG