Auto login for debugging ASP.NET MVC 5

Viewed 2955

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?

3 Answers
Related