views:

1056

answers:

8

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?

A: 

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?

orthod0ks
A: 

a small page that posts itself onload can do the trick

copy and modify a new page for each account needed

it need not be part of your codebase

Jim
A: 

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.

Juri
A: 

In the page_load event you can use FormsAuthentication.SetAuthCookie:

FormsAuthentication.SetAuthCookie("username", false);
I tried this method but the login page still gets tripped.
CharlieG
+2  A: 

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.

Jeff Siver
A: 

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

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

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

Marko
Almost there; I do get authenticated, but the Login.aspx page still gets tripped and prompts me for credentials.
CharlieG
Is your login page the default page? Because thats what I am thinking it is a problem...try to setup some other page as default and set authentication to redirect to login page only if user is not authenticated...cheers
Marko
A: 

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.

Flynn