我有一个ASP.NET应用程序,它使用进行用户身份验证。我的数据库里没有任何密码。虽然我确实使用asp标识存储角色和声明。
我需要一种方式,使我可以“登录”作为任何用户进行测试。
主计长:
#if DEBUG
[AllowAnonymous]
[HttpGet]
public ActionResult LoginOffline()
{
if (Request.IsLocal == false)
{
return HttpNotFound();
}
List<ApplicationUser> model = UserManager.Users.ToList();
return View(model);
}
[AllowAnonymous]
[HttpPost]
public async Task<ActionResult> LoginOffline(string id)
{
if (Request.IsLocal == false)
return HttpNotFound();
ApplicationUser user = UserManager.FindById(id);
if (user != null)
{
await SignInManager.SignInAsync(user, true, true);
}
List<ApplicationUser> model = UserManager.Users.ToList();
return View(model);
}
#endif如您所见,在调试模式下打开项目时,请求必须是本地的。
@model System.Collections.Generic.List<ApplicationUser>
<h2>Super Secret Login Page</h2>
<h3> Currently Logged in as @User.Identity.Name</h3>
@using (Html.BeginForm("LoginOffline", "Account", new {ViewBag.ReturnUrl}))
{
foreach (var user in Model)
{
<div class="row form-group col-lg-offset-1">
<button type="submit" class="btn btn-primary pull-left" id="@user.Id" name="id" value="@user.Id" title="Login as @user.FirstName @user.LastName">Sign in as @user.UserName</button>
<span>
@foreach (var role in user.Roles)
{
@role
@Html.Raw(", ");
}
</span>
</div>
}
}问题
为什么我在第二次尝试中使用的代码示例不是第一次,我如何纠正呢?
更新
添加RedirectToAction
[AllowAnonymous]
[HttpPost]
public async Task<ActionResult> LoginOffline(string id)
{
if (Request.IsLocal == false) return HttpNotFound();
ApplicationUser user = UserManager.FindById(id);
if (user != null)
{
await SignInManager.SignInAsync(user, true, true);
}
return RedirectToAction("LoginOffline");
}这个代码现在起作用了,但我仍然不明白为什么原来的方法失败了?
发布于 2018-03-08 21:09:07
而不是您的POST方法return View(model); --您可以尝试:
return RedirectToAction("LoginOffline");
https://stackoverflow.com/questions/49180336
复制相似问题