• ASP.NET登录记住用户名


    案例如下:

    1:首先在登录的控制器中定义一个全局变量

            public const string LonginName = "sessName";

    2:在登陆的方法中

      public ActionResult Login(string userNameOrEmailAddress = "", string returnUrl = "", string successMessage = "")
            {
                if (string.IsNullOrWhiteSpace(returnUrl))
                    returnUrl = Url.Action("Index", "Application");

                ViewBag.ReturnUrl = returnUrl == "/" ? "" : returnUrl;
                ViewBag.IsMultiTenancyEnabled = _multiTenancyConfig.IsEnabled;

    var model = new LoginFormViewModel();           

        HttpCookie remCookie = HttpContext.Request.Cookies[LonginName];
     
                if (remCookie != null)
                {
                    model.RemberName = remCookie.Value;
                }
                else
                {
                    model.RemberName = "";
                }

                model.TenancyName = _tenancyNameFinder.GetCurrentTenancyNameOrNull();
                model.IsSelfRegistrationEnabled = true;
                model.SuccessMessage = successMessage;
                model.UserNameOrEmailAddress = userNameOrEmailAddress;
                return View(model);
            }

    3:在登陆成功后记录Cookie

            /// <summary>
            /// 注入登录信息到Cookie
            /// </summary>
            /// <returns></returns>
            private async Task SignInAsync(ClaimsIdentity identity = null, bool rememberMe = false)
            {

                HttpCookie remCookie = HttpContext.Request.Cookies[LonginName];
                // 登录成功后记录Session
                if (rememberMe)
                {
                    HttpCookie cook = new HttpCookie(LonginName);
                    cook.Expires.AddDays(7);
                    cook.Value = identity.Name;
                    HttpContext.Response.SetCookie(cook);
                }
                else
                {
                    if (remCookie != null)
                    {
                        remCookie.Value = null;
                        HttpContext.Response.SetCookie(remCookie);
                    }
                }
            }

     

  • 相关阅读:
    [转]oracle 10g数据泵之impdp-同时导入多个文件
    IMP数据到指定的表空间
    [转]Oracle数据泵的使用
    [转]oracle pump expdp impdp使用
    liunx 安装ActiveMQ 及 spring boot 初步整合 activemq
    安装本地jar包到仓库
    centos7.4 64位安装 git
    出现 CannotAcquireLockException 异常
    centos7.4 64位安装 redis-4.0.0
    java代码定时备份mysql数据库及注意事项——基于 springboot
  • 原文地址:https://www.cnblogs.com/wendj/p/6772613.html
Copyright © 2020-2023  润新知