熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

單點登錄在ASP.NET上的簡單實現[3]

2013-11-13 11:00:16  來源: .NET編程 
    在代碼中Security是通過Hash一個隨機產生的數字生成的具有不確定性和保密性我們可以看到Security同時保存在Session中和發送給Service我們把這個Security當作明文在後面我們可以看到Security在Service經過再一次Hash後作為密文發送回Shop如果我們將Session保存的Security經過同樣的Hash方法處理後等到的字符串如果和Service返回的密文相同我們就能夠在一定程度上保證Service應答的數據是沒有經過修改的

using System;
using SystemWeb;
using SystemSecurityCryptography;
using SystemText;

namespace AmethystureSSOShop
{
 public class Page : SystemWebUIPage
 {
  private void CustomerValidate()
  {
   bool Pass = (bool) thisSession[Pass];
   if (!Pass)
   {
    string Security = ;
    Random Seed = new Random();
    Security = SeedNext( intMaxValue)ToString();
    byte[] Value;
    UnicodeEncoding Code = new UnicodeEncoding();
    byte[] Message = CodeGetBytes(Security);
    SHAManaged Arithmetic = new SHAManaged();
    Value = ArithmeticComputeHash(Message);
    Security = ;
    foreach(byte o in Value)
    {
     Security += (int) o + O;
    }
    thisSession[Security] = Security;
    thisSession[Url] = thisRequestRawUrl;
    thisResponseRedirect(ProjectService + /Validateaspx?WebSite= + ProjectWebSite + &Security= + Security);
   }
  }

  protected virtual void Initialize()
  {
   thisResponseWrite(<html>);
   thisResponseWrite(<head>);
   thisResponseWrite(<title>Amethysture SSO Project</title>);
   thisResponseWrite(<link rel=stylesheet type=\text/css\ href=\ + projectwebsite + /Defaultcss\);
   thisResponseWrite(</head>);
   thisResponseWrite(<body>);
   thisResponseWrite(<iframe width=\\ height=\\ src=\ + projectservice + /Customeraspx\></iframe>);
   thisResponseWrite(<div align=\center\);
   thisResponseWrite(Amethysture SSO Shop Any Page);
   thisResponseWrite(</div>);
   thisResponseWrite(</body>);
   thisResponseWrite(</html>);
  }

  protected override void OnInit(EventArgs e)
  {
   baseOnInit(e);
   thisCustomerValidate();
   thisInitialize();
   thisResponseEnd();
  }
 }
}

  Service的Globalcs

  現在我們頁面轉到了Service的Validate頁面我們轉過來看Service的代碼在Global中我們同樣定義了四個Session變量都和Shop的Session用處類似WebSite是保存請求用戶即時狀態的站點信息以便能在登錄後返回正確的請求站點

protected void Session_Start(Object sender EventArgs e)
{
 thisSessionAdd(UserID );
 thisSessionAdd(Pass false);
 thisSessionAdd(WebSite );
 thisSessionAdd(Security );
}

[]  []  []  []  []  


From:http://tw.wingwit.com/Article/program/net/201311/14963.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.