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

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

问题描述

我正在使用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>

在登录"视图中,添加以下内容:

In Login view, add this:

<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身份2:捕获用户对“记住我"的偏好.在ExternalLogin中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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