MVC 5 ASP.NET Identity 2: Capture user's preference for “remember me” in ExternalLogin

后端 未结 2 1675
广开言路
广开言路 2021-01-20 14:29

I am using the Identity 2.0 Sample.

I get that by setting isPersistent to true in ExternalLoginCallback action method, the browser will automatically log the user in

2条回答
  •  感情败类
    2021-01-20 15:01

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

    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 ExternalLoginCallback(string returnUrl, string remembermeExtnl)
        {
        ...
            var result = await SignInHelper.ExternalSignIn(loginInfo, isPersistent: remembermeExtnl=="t"); 
        ...
        }
    

    In Login view, edit to match:

    @Html.Partial("_ExternalLoginsListPartial", new PG.Models.ExternalLoginViewModel() { Action = "ExternalLogin", ReturnUrl = ViewBag.ReturnUrl, RemembermeExtnl = ViewBag.RemembermeExtnl }) Remember me

    In Login view, add this:

    
    

    In _ExternalLoginListPartial:

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

提交回复
热议问题