利用cookie的跨域单点登录的简单实现
生活随笔
收集整理的這篇文章主要介紹了
利用cookie的跨域单点登录的简单实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Configuration: 1. Web.Config 在兩個站點的配置配置文件machine節點上相同的validationKey, decryptionKey and validation的值,如 <machineKey validationKey="282487E295028E59B8F411ACB689CCD6F39DDD21E6055A3EE480424315994760A
DF21B580D8587DB675FA02F79167413044E25309CCCDB647174D5B3D0DD9141"
decryptionKey="8B6697227CBCA902B1A0925D40FAA00B353F2DF4359D2099"
validation="SHA1" /> 2. IIS 在IIS->Directory security 上添加 "ASPNET Machine Account" 的所有權限 Coding: 代碼示例 站點1的->Login.aspx.cs if (login_Successful) { //創建一個cookie HttpCookie cookie = new HttpCookie("strCookieName"); //設置cookie的值? 可以保存登錄名稱信息 cookie.Value ="set_cookie_value"; //設置 cookie 的生存周期5 分鐘 DateTime dtNow = DateTime.Now; TimeSpan tsMinute = new TimeSpan(0, 0, 5, 0); cookie.Expires = dtNow + tsMinute; //添加cookie Response.Cookies.Add(cookie); Response.Write("Cookie written. "); } 檢查cookie是否存在 站點2->Default.aspx.cs protected void Page_Load(object sender, EventArgs e) { //獲取cookie HttpCookie cookie = Request.Cookies["strCookieName"]; //檢查cookie 是否存在 if (cookie != null) { ReadCookie(); } else { lblCookie.Text = "Cookie not found. "; } } protected void ReadCookie() { //Get the cookie name the user entered //Grab the cookie HttpCookie cookie = Request.Cookies["strCookieName"]; //Check to make sure the cookie exists if (cookie == null) { lblCookie.Text = "Cookie not found. "; } else { //Write the cookie value String strCookieValue = cookie.Value.ToString(); lblCookie.Text = "The cookie contains: " + strCookieValue + ""; } }
DF21B580D8587DB675FA02F79167413044E25309CCCDB647174D5B3D0DD9141"
decryptionKey="8B6697227CBCA902B1A0925D40FAA00B353F2DF4359D2099"
validation="SHA1" /> 2. IIS 在IIS->Directory security 上添加 "ASPNET Machine Account" 的所有權限 Coding: 代碼示例 站點1的->Login.aspx.cs if (login_Successful) { //創建一個cookie HttpCookie cookie = new HttpCookie("strCookieName"); //設置cookie的值? 可以保存登錄名稱信息 cookie.Value ="set_cookie_value"; //設置 cookie 的生存周期5 分鐘 DateTime dtNow = DateTime.Now; TimeSpan tsMinute = new TimeSpan(0, 0, 5, 0); cookie.Expires = dtNow + tsMinute; //添加cookie Response.Cookies.Add(cookie); Response.Write("Cookie written. "); } 檢查cookie是否存在 站點2->Default.aspx.cs protected void Page_Load(object sender, EventArgs e) { //獲取cookie HttpCookie cookie = Request.Cookies["strCookieName"]; //檢查cookie 是否存在 if (cookie != null) { ReadCookie(); } else { lblCookie.Text = "Cookie not found. "; } } protected void ReadCookie() { //Get the cookie name the user entered //Grab the cookie HttpCookie cookie = Request.Cookies["strCookieName"]; //Check to make sure the cookie exists if (cookie == null) { lblCookie.Text = "Cookie not found. "; } else { //Write the cookie value String strCookieValue = cookie.Value.ToString(); lblCookie.Text = "The cookie contains: " + strCookieValue + ""; } }
轉載于:https://www.cnblogs.com/linzheng/archive/2010/12/14/1906026.html
總結
以上是生活随笔為你收集整理的利用cookie的跨域单点登录的简单实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Web.Config文件配置之数据库连接
- 下一篇: 热敏打印机应用笔记