跨網站登入機制

出自ProgWiki

跳轉到: 導航, 搜尋

跨網站登入機制(Single sign on,簡稱SSO),參照:『維基百科~Single_sign-on

目錄

用途

檔案

UserData.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Threading;
 
/// <summary>
/// UserData 的摘要描述
/// </summary>
public class UserData
{
	public UserData()
	{
		//
		// TODO: 在此加入建構函式的程式碼
		//
	}
 
	public Mutex SyncLock = new Mutex();
	public string strUserID;
	public string strPassWord;
	public string strLoginID;
	public Dictionary<string, string> VarBuff = new Dictionary<string, string>(); 
}

CrossSiteLogin.asmx

<%@ WebService Language="C#" CodeBehind="~/App_Code/CrossSiteLogin.cs" Class="CrossSiteLogin" %>

CrossSiteLogin.cs

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Collections.Generic;
 
 
/// <summary>
/// CrossSiteLogin 的摘要描述
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class CrossSiteLogin : System.Web.Services.WebService {
 
    public CrossSiteLogin () {
 
        //如果使用設計的元件,請取消註解下行程式碼 
        //InitializeComponent(); 
    }
 
	public static Dictionary<string, UserData> LoginUser = new Dictionary<string, UserData>();
 
 
	//a.登入 
	[WebMethod]
	public string Login(string strUserID, string strPassWord)
	{
		string strLoginID = "";
		bool IsLogined = false;
 
		//判斷登入是否成功的地方
		//略(請根據實際的登入機制改寫下列判斷式
		if ((strUserID == "Test") & (strPassWord == "Test")) //測試用
		{
			IsLogined = true;
		}
 
		if (IsLogined == true)
		{
			strLoginID = Guid.NewGuid().ToString();
 
			UserData user = new UserData();
			user.strUserID = strUserID;
			user.strPassWord = strPassWord;
			user.strLoginID = strLoginID;
 
			LoginUser.Add(strLoginID, user);
		}
 
 
		return strLoginID;
	}
 
	//b.驗証是否已登入
	[WebMethod]
	public bool CheckLogined(string strLoginID)
	{
		try
		{
			return LoginUser.ContainsKey(strLoginID);
		}
		catch
		{
			return false;
		}
	}
 
	//c.登出
	[WebMethod]
	public bool LogOut(string strLoginID)
	{
		try
		{
			LoginUser.Remove(strLoginID);
			return true;
		}
		catch
		{
			return false;
		}
	}
 
	//d.Set共用變數 
	[WebMethod]
	public bool SetVarValue(string strLoginID, string strVarName, string strValue)
	{
		try
		{
			UserData user = LoginUser[strLoginID];
			user.SyncLock.WaitOne();
				user.VarBuff.Add(strVarName, strValue);
			user.SyncLock.ReleaseMutex();
			return true;
		}
		catch
		{
			return false;
		}
	}
 
	//e.Get共用變數 
	[WebMethod]
	public string GetVarValue(string strLoginID, string strVarName)
	{
		try
		{
			UserData user = LoginUser[strLoginID];
			user.SyncLock.WaitOne();
				string strVarValue = user.VarBuff[strVarName];
			user.SyncLock.ReleaseMutex();
			return strVarValue;
		}
		catch
		{
			return "";
		}
	}
 
}

相關參考

個人工具
名字空間
變換
動作
導航
分類
其他
技術類News或部落格
工具箱