網上看到很多單點登錄的原理和實例,講的像廣告一樣。哎,我看了好幾天還是看不明白。但是項目需要這個功能,于是,我自己做了一個。發布出來,希望像我一樣的需求的人能早些解決問題。
思路:使用第三方認證。那么就要用到跨域訪問(就是變量要在不同ip地址下實現訪問),大白話說,就是網絡訪問。我不會Socket,就用Remoting吧!(Application不可以,因為它不能跨主機)
看代碼:
1,建類庫StatusService,在它里面建類LoginList
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace StatusService
{
??? public class LoginList : MarshalByRefObject, IList
??? {
??????? private object[] _contents = new object[8];
??????? private int _count;
??????? public LoginList()
??????? {
??????????? _count = 0;
??????? }
??????? // IList Members
??????? public int Add(object value)
??????? {
??????????? if (_count < _contents.Length)
??????????? {
??????????????? _contents[_count] = value;
??????????????? _count++;
??????????????? return (_count - 1);
??????????? }
??????????? else
??????????? {
??????????????? return -1;
??????????? }
??????? }
??????? public void Clear()
??????? {
??????????? _count = 0;
??????? }
??????? public bool Contains(object value)
??????? {
??????????? bool inList = false;
??????????? for (int i = 0; i < Count; i++)
??????????? {
??????????????? if (_contents[i] == value)
??????????????? {
??????????????????? inList = true;
??????????????????? break;
??????????????? }
??????????? }
??????????? return inList;
??????? }
??????? public int IndexOf(object value)
??????? {
??????????? int itemIndex = -1;
??????????? for (int i = 0; i < Count; i++)
??????????? {
??????????????? if (_contents[i] == value)
??????????????? {
??????????????????? itemIndex = i;
??????????????????? break;
??????????????? }
??????????? }
??????????? return itemIndex;
??????? }
??????? public void Insert(int index, object value)
??????? {
??????????? if ((_count + 1 <= _contents.Length) && (index < Count) && (index >= 0))
??????????? {
??????????????? _count++;
??????????????? for (int i = Count - 1; i > index; i--)
??????????????? {
??????????????????? _contents[i] = _contents[i - 1];
??????????????? }
??????????????? _contents[index] = value;
??????????? }
??????? }
??????? public bool IsFixedSize
??????? {
??????????? get
??????????? {
??????????????? return true;
??????????? }
??????? }
??????? public bool IsReadOnly
??????? {
??????????? get
??????????? {
??????????????? return false;
??????????? }
??????? }
??????? public void Remove(object value)
??????? {
??????????? RemoveAt(IndexOf(value));
??????? }
??????? public void RemoveAt(int index)
??????? {
??????????? if ((index >= 0) && (index < Count))
??????????? {
??????????????? for (int i = index; i < Count - 1; i++)
??????????????? {
??????????????????? _contents[i] = _contents[i + 1];
??????????????? }
??????????????? _count--;
??????????? }
??????? }
??????? public object this[int index]
??????? {
??????????? get
??????????? {
??????????????? return _contents[index];
??????????? }
??????????? set
??????????? {
??????????????? _contents[index] = value;
??????????? }
??????? }
??????? // ICollection Members
??????? public void CopyTo(Array array, int index)
??????? {
??????????? int j = index;
??????????? for (int i = 0; i < Count; i++)
??????????? {
??????????????? array.SetValue(_contents[i], j);
??????????????? j++;
??????????? }
??????? }
??????? public int Count
??????? {
??????????? get
??????????? {
??????????????? return _count;
??????????? }
??????? }
??????? public bool IsSynchronized
??????? {
??????????? get
??????????? {
??????????????? return false;
??????????? }
??????? }
??????? // Return the current instance since the underlying store is not
??????? // publicly available.
??????? public object SyncRoot
??????? {
??????????? get
??????????? {
??????????????? return this;
??????????? }
??????? }
??????? // IEnumerable Members
??????? public IEnumerator GetEnumerator()
??????? {
??????????? // Refer to the IEnumerator documentation for an example of
??????????? // implementing an enumerator.
??????????? throw new Exception("The method or operation is not implemented.");
??????? }
??????? public void PrintContents()
??????? {
??????????? Console.WriteLine("List has a capacity of {0} and currently has {1} elements.", _contents.Length, _count);
??????????? Console.Write("List contents:");
??????????? for (int i = 0; i < Count; i++)
??????????? {
??????????????? Console.Write(" {0}", _contents[i]);
??????????? }
??????????? Console.WriteLine();
??????? }
??? }
}
?2,建一個控制臺SSOService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
namespace SSOService
{
??? class MyServer
??? {
??????? [STAThread]
??????? static void Main(string[] args)
??????? {
??????????? RemotingConfiguration.Configure("SSOService.exe.config");
??????????? Console.ReadLine();
??????? }
??? }
}
配置文件是:
<configuration>
? <system.runtime.remoting>
??? <application name="SSOService">
????? <service>
??????? <wellknown type="StatusService.LoginList,StatusService" objectUri="StatusService.LoginList"
??????????? mode="Singleton" />
????? </service>
????? <channels>
??????? <channel ref="tcp" port="9999"/>
????? </channels>
??? </application>
? </system.runtime.remoting>
</configuration>
3,網站S1文件:
Default.aspx
Login.aspx
Web.config
(1)Default.aspx內容就是:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns="
http://www.w3.org/1999/xhtml
">
<head runat="server">
??? <title>S2</title>
</head>
<body>
??? <form id="form1" runat="server">
??? <div>
??? S2登陸成功!
??? </div>
??? </form>
</body>
</html>
實際使用按照需求自由調整。
(2)Login.aspx
前臺是:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns="
http://www.w3.org/1999/xhtml
">
<head runat="server">
??? <title>S2</title>
</head>
<body>
??? <form id="form1" runat="server">
??? <div style="line-height:30px;">
??? <div>s2登陸界面</div>
??? 用戶:<asp:TextBox id="user" Text="abc" runat="server"></asp:TextBox><br />
??? 密碼:<asp:TextBox ID="pwd" Text="123" runat="server"></asp:TextBox><br />
??? <asp:Button ID="Button1" Text="登陸" OnClick="Button1_OnClick" runat="server" />
??? </div>
??? </form>
</body>
</html>
后臺是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Login : System.Web.UI.Page
{
??? StatusService.LoginList ls = (StatusService.LoginList)Activator.GetObject(typeof(StatusService.LoginList), System.Configuration.ConfigurationSettings.AppSettings["ServiceURL"]);
??? protected void Page_Load(object sender, EventArgs e)
??? {
??????? Response.Write("記錄條數是:" + ls.Count.ToString() + "<br/>");
??????? for (int i = 0; i < ls.Count; i++)
??????? {
??????????? //Response.Write(ls[i] + "<br/>");
??????????? if (ls[i].ToString().Trim()=="abc")
??????????? {
??????????????? Response.Redirect("Default.aspx");
??????????? }
??????? }
??? }
??? protected void Button1_OnClick(object sender, EventArgs e)
??? {
??????? ls.Add("abc");
??????? Response.Redirect("Default.aspx");
??? }
}
(3)Web.config
<?xml version="1.0"?>
<configuration>
?<appSettings>
??<add key="ServiceURL" value="tcp://localhost:9999/StatusService.LoginList"/>
?</appSettings>
?<system.web>
??<compilation debug="true" targetFramework="4.0"/>
?</system.web>
</configuration>
4,再建一個S2,和S1一樣。
5,運行時要設置多項目運行,并且是最先運行SSOService。
完畢!
上敘是在vs2010上測試的。絕對簡單實用,沒有廣告!不實就砸我吧,我認!有好的建議希望多賜教!謝謝了!
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
