net core建站

來源:互聯網
上載者:User

標籤:cookies   table   伺服器   decrypt   c++   方法調用   null   .com   one   

net core建站踩坑記錄

系統:win10
VS版本:2017
.NET Core 版本: 1.1

零.讀取設定檔

參考:http://www.tuicool.com/articles/QfYVBvi

  1. 此版本無需添加其他組件
  2. appsettings.json配置中添加節點AppSettings
  3. 添加設定檔的映射模型
  4. 在Startup.cs ConfigureServices方法中註冊

        services.AddOptions();    services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
  5. Controller中使用

一、登入記錄session

參考:http://www.cnblogs.com/fonour/p/5943401.html

二、發布.net core1.1.2網站到windos伺服器

參考:https://docs.microsoft.com/en-us/aspnet/core/publishing/iis

  1. 我的伺服器是windows server 2012 ,.net core網站版本為1.1.2
  2. 經安裝好iis
  3. 下載安裝:
    .NET Core Windows Server Hosting
    Microsoft Visual C++ 2015 Redistributable Update 3
  4. 發布.net core網站到IIS,並將應用池的.NET CLR版本修改為[無Managed 程式碼]
三、DES加密解密演算法

親測可用

  public class SecurityHelper  {      #region 加密解密法一      //預設密鑰向量       private static byte[] Keys = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };      /// <summary>       /// DES加密字串       /// </summary>       /// <param name="encryptString">待加密的字串</param>       /// <param name="encryptKey">加密金鑰,要求為16位</param>       /// <returns>加密成功返回加密後的字串,失敗返回源串</returns>       public static string EncryptDES(string encryptString, string encryptKey = "Key123Ace#321Key")      {          try          {              byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 16));              byte[] rgbIV = Keys;              byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);              var DCSP = Aes.Create();              MemoryStream mStream = new MemoryStream();              CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);              cStream.Write(inputByteArray, 0, inputByteArray.Length);              cStream.FlushFinalBlock();              return Convert.ToBase64String(mStream.ToArray());          }          catch (Exception ex)          {              return ex.Message + encryptString;          }      }      /// <summary>       /// DES解密字串       /// </summary>       /// <param name="decryptString">待解密的字串</param>       /// <param name="decryptKey">解密密鑰,要求為16位,和加密金鑰相同</param>       /// <returns>解密成功返回解密後的字串,失敗返源串</returns>       public static string DecryptDES(string decryptString, string decryptKey = "Key123Ace#321Key")      {          try          {              byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 16));              byte[] rgbIV = Keys;              byte[] inputByteArray = Convert.FromBase64String(decryptString);              var DCSP = Aes.Create();              MemoryStream mStream = new MemoryStream();              CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);              Byte[] inputByteArrays = new byte[inputByteArray.Length];              cStream.Write(inputByteArray, 0, inputByteArray.Length);              cStream.FlushFinalBlock();              return Encoding.UTF8.GetString(mStream.ToArray());          }          catch (Exception ex)          {              return ex.Message + decryptString;          }      }      #endregion   }
四、過濾器定義

繼承Attribute,實現IActionFilter即可
簡單校正登入,擷取cookie值並解密後得到使用者名稱,未登入則跳轉登入(ApplicationKey為自訂的類存放)

public class UserCheckFilterAttribute : Attribute, IActionFilter{    public void OnActionExecuted(ActionExecutedContext context)    {    }    public void OnActionExecuting(ActionExecutingContext filterContext)    {        string encryptValue = "";        filterContext.HttpContext.Request.Cookies.TryGetValue(ApplicationKey.User_Cookie_Key, out encryptValue);        if (encryptValue == null)        {            filterContext.Result = new RedirectResult("/Account/Login");            return;        }        var userName = SecurityHelper.DecryptDES(encryptValue, ApplicationKey.User_Cookie_Encryption_Key);        if (string.IsNullOrEmpty(userName))        {            filterContext.Result = new RedirectResult("/Account/Login");            return;        }    }}
五、注入服務

Startup.cs中的ConfigureServices方法調用services.AddTransient<IUserService,UserService>();註冊服務

待解決問題

根據路徑呼叫指令碼

net core建站

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.