首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于开发环境的ASP.NET MVC 5 SignIn BackDoor

用于开发环境的ASP.NET MVC 5 SignIn BackDoor
EN

Stack Overflow用户
提问于 2018-03-08 18:46:08
回答 1查看 191关注 0票数 0

我有一个ASP.NET应用程序,它使用进行用户身份验证。我的数据库里没有任何密码。虽然我确实使用asp标识存储角色和声明。

我需要一种方式,使我可以“登录”作为任何用户进行测试。

主计长:

代码语言:javascript
复制
#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

如您所见,在调试模式下打开项目时,请求必须是本地的。

代码语言:javascript
复制
@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

代码语言:javascript
复制
        [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");
    }

这个代码现在起作用了,但我仍然不明白为什么原来的方法失败了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-08 21:09:07

而不是您的POST方法return View(model); --您可以尝试:

return RedirectToAction("LoginOffline");

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49180336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档