zoukankan      html  css  js  c++  java
  • asp.net.core网站重启后登陆无效问题(部署在IIS)

      一.问题

      在使用asp.net.core时,把网站发布到IIS后,在后续更新中需要停止网站,然后重启网站,发现已经登陆的用户会退出登陆。过程如下

      1.登陆代码(测试)

     1 [AllowAnonymous]
     2         [HttpGet]
     3         public IActionResult Login([FromServices] ILogger<AccountController> logger)
     4         {
     5             if (User.Identity.IsAuthenticated) //已经登陆跳转
     6             {
     7                 logger.LogInformation("有登陆信息");
     8                 return Redirect("/Home/Index");
     9             }
    10             else
    11             {
    12                 logger.LogInformation("无登陆信息");
    13             }
    14             return View();
    15         }
    16         [AllowAnonymous]
    17         [HttpPost]
    18         public async Task<IActionResult> Login(string acount,string password)
    19         {
    20             if (acount == "111" && password == "111")
    21             {
    22                 var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
    23                 identity.AddClaim(new Claim(ClaimTypes.Sid,acount));
    24                 await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
    25                 return Redirect("/Home/Index");
    26             }
    27             return View();
    28         }

       2.先登陆网站,在iis内把网站先停止,再启动。现在刷新网页,发现会自动退出登陆,F12查看cookie信息如下

      

    可以看到已经跳转到了登陆页面,cookie信息并没有过期

      二.原因

      原因是什么,找了很久原来是.netcore的数据保护问题 详情可以看官方说明 https://docs.microsoft.com/zh-cn/aspnet/core/security/data-protection/introduction?view=aspnetcore-2.2

      简单的来说就是在默认情况下程序每次启动时会生成一个密钥 key, 当你把程序停止时 这个key就会失去,再启动时生成一个新的key,所以之前的登陆信息就会失效。

      三.解决方法

      解决方法:把密钥持久化,可以使用 数据库,redis,文件系统,自定义存储等等方法,对应的方法使用说明:https://docs.microsoft.com/zh-cn/aspnet/core/security/data-protection/implementation/key-storage-providers?view=aspnetcore-2.2&tabs=visual-studio

      简单介绍下文件系统使用

    1      services.AddDataProtection()
    2                 .PersistKeysToFileSystem(new DirectoryInfo(@"d:	emp-keys"));

      程序首次启动会在对应文件夹下创建文件,后续就会一直读取文件中的密钥

      密钥文件如下(xml文件)

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <key id="780a229b-dac4-413f-8e7d-1467b2c4997a" version="1">
     3   <creationDate>2019-03-21T06:31:35.979Z</creationDate>
     4   <activationDate>2019-03-21T06:31:35.947Z</activationDate>
     5   <expirationDate>2019-06-19T06:31:35.947Z</expirationDate>
     6   <descriptor deserializerType="Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60">
     7     <descriptor>
     8       <encryption algorithm="AES_256_CBC" />
     9       <validation algorithm="HMACSHA256" />
    10       <masterKey p4:requiresEncryption="true" xmlns:p4="http://schemas.asp.net/2015/03/dataProtection">
    11         <!-- Warning: the key below is in an unencrypted form. -->
    12         <value>D3CSJaTNFoTMqSwRXxQDn36+12Jv5vRA/MVRezbue2CxP5YAJaiCVVrbOS/MPu5UBdOAObPRWz5jUbKAaumjmg==</value>
    13       </masterKey>
    14     </descriptor>
    15   </descriptor>
    16 </key>

    这是生成的文件 包含激活时间,过期时间,加密方法以及密钥等

     其中还有更多操作大家可以查看官方文档

  • 相关阅读:
    C#使用Json
    JQuery AJAX介绍
    封装一个自己的 Ajax小框架
    AJAX跨域实现
    AJAX 一些常用方法
    完整的 AJAX 写法(支持多浏览器)
    AJAX 简单上手
    利用反射生成SQL语句
    访问Access数据库(有多个数据库时 体现多态)
    DataTable操作(建表,建行,建列,添加数据)
  • 原文地址:https://www.cnblogs.com/linxingxunyan/p/10571247.html
Copyright © 2011-2022 走看看