MVC 5 ASP.NET Identity 2:捕获用户对“记住我"的偏好在外部登录 [英] MVC 5 ASP.NET Identity 2: Capture user's preference for "remember me" in ExternalLogin

查看:23
本文介绍了MVC 5 ASP.NET Identity 2:捕获用户对“记住我"的偏好在外部登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Identity 2.0 示例.

I am using the Identity 2.0 Sample.

我通过在 ExternalLoginCallback 操作方法中将 isPersistent 设置为 true 得到了这一点,浏览器将在用户下次(在限制范围内)使用同一浏览器访问时自动登录.我知道如果用户的记住我"首选项被捕获并传递给 ExternalLogin 操作方法,它可以放入 returnUrl 并在 ExternalLoginCallback 中访问.但是我如何让他们更喜欢 ExternalLogin 操作方法?

I get that by setting isPersistent to true in ExternalLoginCallback action method, the browser will automatically log the user in the next time (within limits) they visit using the same browser. I know that if the user's "remember me" preference was captured and passed to the ExternalLogin action method that it could be put into returnUrl and accessed in ExternalLoginCallback. But how do I get their preference to the ExternalLogin action method?

在这种情况下,我不知道如何在 LoginView 页面上放置一个复选框并将其连接起来,以便我可以在 ExternalLogin 操作方法中处理它.我怎样才能做到这一点?

I don't get in this case how to put a checkbox on the LoginView page and wire things up so that I can process it in the ExternalLogin action method. How can I accomplish this?

推荐答案

不要删除任何代码,只需更改如下:

Don't delete any code, just change as follows:

在 AccountViewModels 中,编辑以匹配:

In AccountViewModels, edit to match:

public class ExternalLoginViewModel 
    {
        public string Action { get; set; }
        public string ReturnUrl { get; set; }
        public string RemembermeExtnl { get; set; }
    }

在帐户控制器中,编辑以匹配:

In Account Controller, edit to match:

public ActionResult Login(string returnUrl)
    {   
        ViewBag.ReturnUrl = returnUrl;
        ViewBag.RemembermeExtnl = "f";
        return View();          
    }

public ActionResult ExternalLogin(string provider, string returnUrl, string remembermeExtnl) 
    {
        // Request a redirect to the external login provider
        return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl, remembermeExtnl = remembermeExtnl }));
    }


public async Task<ActionResult> ExternalLoginCallback(string returnUrl, string remembermeExtnl)
    {
    ...
        var result = await SignInHelper.ExternalSignIn(loginInfo, isPersistent: remembermeExtnl=="t"); 
    ...
    }

在登录视图中,编辑以匹配:

In Login view, edit to match:

<section id="socialLoginForm">
    @Html.Partial("_ExternalLoginsListPartial", new PG.Models.ExternalLoginViewModel() { Action = "ExternalLogin", ReturnUrl = ViewBag.ReturnUrl, RemembermeExtnl = ViewBag.RemembermeExtnl })                
     <input type="checkbox" id="cbxRemExt" />  Remember me  
</section>

在登录视图中,添加:

<script>
// ** change to eq(1) (2 places!) if your social login form is the second form on the page,
//  keep as below if first form is your social login form **
  $("#cbxRemExt").change(function () {
      var isChecked = $(this).is(":checked");
       var actionstring = $('form').eq(0).attr('action');          
       actionstring = actionstring.replace('RemembermeExtnl=' + (isChecked ? 'f' : 't'), 'RemembermeExtnl=' + (isChecked ? 't' : 'f'))
       $('form').eq(0).attr('action', actionstring);
  });
</script>

在_ExternalLoginListPartial:

In _ExternalLoginListPartial:

string action = Model.Action;
string returnUrl = Model.ReturnUrl;
string remembermeExtnl = Model.RemembermeExtnl;
using (Html.BeginForm(action, "Account", new { ReturnUrl = returnUrl, RemembermeExtnl = remembermeExtnl }))

这篇关于MVC 5 ASP.NET Identity 2:捕获用户对“记住我"的偏好在外部登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆