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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.

Method 3

In the Application_AuthenticateRequest method (aka the Applications AuthenticateRequest event) in the global.asax file, add code that checks if you are running the site within the debugger (something like system.Diagnostics.Debugger.IsAttached) and, if you are, have it create the login ticket, create the cookie and attach it to the session. The FormsAuthentication library provides what you need if hte membership provder doesn’t have it.

Method 4

Could you just put code in the load event to set the username and password strings and fire the onclick event of the login button?

Method 5

As an alternative there exists also tools like Selenium IDE which is a plugin for Firefox. Its main purpose is to provide some kind of testing for UIs. For this purpose you can record actions that are done on the UI. What you could do is to record the credentials you enter for the test-user once and save them. The next time you come back, you execute the script which automatically fills in the necessary information.

There are other tools which are specialized for the purpose of auto-filling a form on a webpage. Selenium is more thought as a testing evironment, but I’ve also used it for such purposes. Of course this is just a workaround.

Method 6

In the page_load event you can use FormsAuthentication.SetAuthCookie:

FormsAuthentication.SetAuthCookie("username", false);

Method 7

The FormsAuthentication.RedirectFromLoginPage uses the defaultUrl to decide where to redirect after login. So make sure, you have a valid url other than your LoginPage.aspx defined.

FormsAuthentication.RedirectFromLoginPage("userName", True);

<authentication mode="Forms">
    <forms loginUrl="~/Account/Login.aspx"
                 ...
                 defaultUrl="~/AnyFolder/PageAfterLogin.aspx"
                 ... />
</authentication>

I hope this helps a bit.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x