[英]Auto login for debugging ASP.NET MVC 5
I've seen 我见过
ASP.NET forms authentication - auto login with a test account while debugging? ASP.NET表单身份验证 - 在调试时使用测试帐户自动登录?
ASP.NET site auto-login during development ASP.NET站点在开发期间自动登录
but that's targeted for older version of ASP.NET MVC (see dates on posts) 但这是针对旧版ASP.NET MVC的目标(请参阅帖子上的日期)
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. 到global.aspx,但没有运气。 The login page still triggers.
登录页面仍然会触发。 I've tried adding a check in Login, and the page never loads
我已经尝试在Login中添加一个检查,页面永远不会加载
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). 但是当我导航到需要身份验证的页面时,网页无限加载(如果我调试它会点击SignInManager.PasswordSignInAsync()并且永远不会返回(AccountController中的自动代码)。
Any idea what's the way to do this in ASP.NET mvc 5? 知道在ASP.NET mvc 5中这样做的方法是什么?
In your HomeController or wherever you'll want your default start URL to be 在您的HomeController中或您想要的默认起始URL的任何位置
#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 并修改您的AccountController以包含
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 :)
请享用 :)
This didnt work for me directly I needed to modify Adams reply to this. 这对我没有用,我需要修改Adams对此的回复。 Im using VS2013 and MVC 5.
我使用的是VS2013和MVC 5。
Change the signature of AutoLogin to this: (note the new 'ActionResult' after Task) 将AutoLogin的签名更改为:(注意任务后的新'ActionResult')
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. 我还将返回URL更改为“Home \\ Index”中的“Home”,但不确定这是否有所作为。
And finally I changed: return Content("Not debugging"); 最后我改变了:return Content(“Not debugging”); to this:
对此:
return View("Index");
Hope this helps someone. 希望这有助于某人。 It works well for me.
这对我来说很有用。 Cheers.Jon
Cheers.Jon
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.