3

I've seen

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

ASP.NET site auto-login during development

but that's targeted for older version of ASP.NET MVC (see dates on posts)

I've tried adding

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
    if (System.Diagnostics.Debugger.IsAttached && User == null)
    {
        FormsAuthentication.SetAuthCookie("user1@contoso.com", true);
    }
}

To global.aspx, but no luck. The login page still triggers. I've tried adding a check in Login, and the page never loads

public ActionResult Login(string returnUrl)
{
    if (System.Diagnostics.Debugger.IsAttached && User.Identity.IsAuthenticated == false)
    {
        var x = new LoginViewModel() { Email = "user1@contoso.com", Password = "Pa55w0rd!", RememberMe = false };
        Login(x, returnUrl).Wait();
        return View(x);
    }

    ViewBag.ReturnUrl = returnUrl;
    return View();
}

but when I navigate to a page with requiring authentication, the web page loads indefinitely (if I debug it hits SignInManager.PasswordSignInAsync() and never returns (autogenned code in AccountController).

Any idea what's the way to do this in ASP.NET mvc 5?

Community
  • 1
  • 1
friartuck
  • 2,954
  • 4
  • 33
  • 67
  • you can achieve this without making any changes in your code, Just webconfig changes and a sample app to login will be sufficient. I can share more details if this solution is feasible for you – Rajshekar Reddy Mar 13 '16 at 07:25

3 Answers3

1

In your HomeController or wherever you'll want your default start URL to be

#if DEBUG
        public async Task AutoLogin()
        {

            if (System.Diagnostics.Debugger.IsAttached)
            {
                var controller = DependencyResolver.Current.GetService();
                controller.InitializeController(Request.RequestContext);
                return await controller.Login(new LoginViewModel() { Email = "user@no.com", Password = "passwordHere", RememberMe = false }, "/Home/Index");
            }

            return Content("Not debugging");
        }
#endif

and modify your AccountController to contain

using System.Web.Routing;

and

#if DEBUG
        public void InitializeController(RequestContext context)
        {
            base.Initialize(context);
        }
#endif

This code will only be included for debug builds as well.

Just tested it, should work OK. Enjoy :)

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
1

This is what did it for me; pretty simple:

public ActionResult Login()
{
  if (System.Diagnostics.Debugger.IsAttached)
  {
    var controller = DependencyResolver.Current.GetService<AccountController>();
    controller.ControllerContext = new ControllerContext(this.Request.RequestContext, controller);
    return controller.Login("toddmo","abc123");
  }
  return View();
}
toddmo
  • 20,682
  • 14
  • 97
  • 107
0

This didnt work for me directly I needed to modify Adams reply to this. Im using VS2013 and MVC 5.

Change the signature of AutoLogin to this: (note the new 'ActionResult' after Task)

public async Task<ActionResult> AutoLogin()

Dependency resolver line needed changed to this:

AccountController controller = (AccountController) DependencyResolver.Current.GetService(typeof (AccountController));

I also changed the return URL to simply "Home" from "Home\Index" not sure if that made a difference though.

And finally I changed: return Content("Not debugging"); to this:

return View("Index");

Hope this helps someone. It works well for me. Cheers.Jon

JonM
  • 1