1.進入管理中心》應用程序管理

2.找到 Secure Store Service 應用程序代理

3.然后就是新建了



5.輸入網站集管理員
6.這個時候SharePoint就知道你需要給OA這個系統做單點登錄了。
7.下一步就是我們要把我們進OA系統的帳號密碼告訴SharePoint,讓他記住當前登錄域賬戶所存的OA賬戶。
8.下面我們模擬一下OA系統登錄頁面
<html>
<body>
<form id=form1 action='dologin.aspx'>
<input type='text' name='name'/>
<input type='password' name='pwd'/><input type='submit' value='login'/>
</form>
</body>
</html>
9.OA的主頁是http://OA/index.aspx 這是一個簡單的登錄頁面代碼,我們從代碼得知,
這個form要提交的頁面是 http://OA/dologin.aspx ,
登錄名name
密碼pwd
那我們登錄也需要這幾個元素。在我們的moss中創建一個列表SSOList。

應用程序名稱,就是一個顯示用的,好讓你知道是啥系統,SSPkey就是我們前面創建的OA 唯一標識。。。你懂得。
那下一步我們就該寫代碼了。
先整理下思路,我要先在SSOList這個列表中讀出來我們登錄的系統。
比如
讀取列表 然后拼出來,這個你懂得,我就不寫了。
我們有OA的登錄信息了,也有在管理中心建立OA了,那一步我們是不是改往SSO里面存儲登錄的帳號密碼了?
比如我們OA的賬戶:zhangsan密碼:123456
這個時候我們創建一個webpart。然后創建一個應用程序頁面 aspx。


這個頁面是專門來存賬戶和密碼的
/// <summary>
/// 在SSO中存儲當前登錄用戶的配置的第三方系統單點登錄的帳號和密碼
/// </summary>
/// <param name="ssosetting"></param>
private void InsertSSO(string ssokey,string loginname,string loginpwd)
{
string userloginname = SPContext.Current.Web.CurrentUser.LoginName;
if (!string.IsNullOrEmpty(ssokey) && !string.IsNullOrEmpty(loginname) && !string.IsNullOrEmpty(loginpwd))
{
string[] userinfo = { loginname, loginpwd};
SetUserCredentials(ssokey, userinfo, userloginname);
}
}
/// <summary>
/// 設置指定用戶的登陸憑據
/// </summary>
/// <param name="appId">業務系統標識</param>
/// <param name="userInfo">憑據信息</param>
/// <param name="userLoginName">MOSS登陸帳號: domainName\LoginName</param>
public static void SetUserCredentials(string appId, string[] userInfo, string userLoginName)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
List<SecureStoreCredential> creds = new List<SecureStoreCredential>();
SecureStoreCredential name = new SecureStoreCredential(toSecureString(userInfo[0]), SecureStoreCredentialType.UserName);
SecureStoreCredential pwd = new SecureStoreCredential(toSecureString(userInfo[1]), SecureStoreCredentialType.Password);
creds.Add(name);
creds.Add(pwd);
SecureStoreCredentialCollection credes = new SecureStoreCredentialCollection(creds.ToArray());
SecureStoreServiceProxy proxySs = new SecureStoreServiceProxy();
SPContext.Current.Site.AllowUnsafeUpdates = true;
SPContext.Current.Web.AllowUnsafeUpdates = true;
SPServiceContext context = SPServiceContext.GetContext(SPContext.Current.Site);
ISecureStore store = proxySs.GetSecureStore(context);
SPClaim claim = SPClaimProviderManager.Local.ConvertIdentifierToClaim(userLoginName, SPIdentifierTypes.WindowsSamAccountName);
store.SetUserCredentials(appId, new SecureStoreServiceClaim(claim), credes);
});
}
catch { }
}
這個方法就會把你當前登錄人對應OA的賬戶密碼儲存在moss的OA里面
OK,現在賬戶也存儲了,列表也有了。下一步就是登錄了。
在新建一個頁面SSOSignOn.aspx

我們不是把列表里面的數據讀出來了嗎?下一步點這個OA連接跳轉到SSOSignOn.aspx頁面。
需要傳幾個參數,登錄請求頁面,賬戶的name值。密碼的name值。登錄成功后跳轉的頁面。雖然我們登錄后,OA系統會自動跳轉到登錄成功頁面,
之所以這么做。是因為我們的單點登錄不是都要登錄OA首頁的,比如我們要直接登錄到OA的其他功能模塊

這個時候我們就不能登錄成功后,還到OA首頁了吧。
言歸正傳,我們SSOSignOn.aspx頁面要處理些什么呢?
模擬OA的登錄頁面登錄
SSOSignOn頁面代碼
<html>
<head id="Head1" runat="server">
<title></title>
<script src="/_layouts/Infinite/js/jquery-1.6.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
//
window.onload = function onsubmita() {
var ishavesso = "<%=this.IsHaveSSO %>";
var xmlhttp;
if (ishavesso == "true") {
var applicationType = "<%=this.AppType%>";
var loginname = $("#UserName").val();
var loginpwd = $("#PassWord").val();
xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
//登錄應用
xmlhttp.Open('POST', '<%=this.GotoUrl %> ', false);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.Send("<%=this.ParmLoginName %>=" + loginname + "&<%=this.ParmPassword %>=" + loginpwd);
document.location = "<%=this.DetailUrl %>";
}
}
</script>
</head>
<body>
<form action="<%=this.GotoUrl %>" method="post" autocomplete="off">
<div id="logindiv" style="text-align: center; vertical-align: middle; height: 700px;
margin-top: 180px; display: none;">
<input type="hidden" name="" id="UserName" runat="server" />
<input type="hidden" name="" id="PassWord" runat="server" />
<br />
</div>
<asp:Label ID="LabMsg" runat="server"></asp:Label>
<input type="hidden" name="return" id="returnPage" runat="server" value="" />
<input name="" id="FormActionValue" runat="server" type="hidden" />
</form>
</body>
</html>
cs文件代碼
private string appname = string.Empty;
public string AppName//SSOKey
{
get
{
if (this.appname == string.Empty && !string.IsNullOrEmpty(this.Request.QueryString["appname"]))
this.appname = this.Request.QueryString["appname"];
return this.appname;
}
}
private string gotourl = string.Empty;
public string GotoUrl//登錄請求地址
{
get
{
if (this.gotourl == string.Empty && !string.IsNullOrEmpty(this.Request.QueryString["gotourl"]))
this.gotourl = HttpUtility.UrlDecode(this.Request.QueryString["gotourl"]);
return this.gotourl;
}
}
private string detailurl = string.Empty;
public string DetailUrl//登錄成功后跳轉的地址
{
get
{
if (this.detailurl == string.Empty && !string.IsNullOrEmpty(this.Request.QueryString["detailurl"]))
this.detailurl = HttpUtility.UrlDecode(this.Request.QueryString["detailurl"]);
return this.detailurl;
}
}
private string parmLoginName = string.Empty;
public string ParmLoginName//帳號name參數
{
get
{
if (this.parmLoginName == string.Empty && !string.IsNullOrEmpty(this.Request.QueryString["pname"]))
this.parmLoginName = this.Request.QueryString["pname"];
return this.parmLoginName;
}
}
private string parmPassword = string.Empty;
public string ParmPassword//密碼name參數
{
get
{
if (this.parmPassword == string.Empty && !string.IsNullOrEmpty(this.Request.QueryString["ppwd"]))
this.parmPassword = this.Request.QueryString["ppwd"];
return this.parmPassword;
}
}
private string _isHaveSSO = string.Empty;//sso里面是否存在賬戶和密碼
public string IsHaveSSO {
get {
return this._isHaveSSO;
}
}
/// <summary>
/// 獲取單點登陸業務系統中當前用戶的信息
/// </summary>
/// <param name="appId">業務系統標識</param>
/// <returns></returns>
public static List<string> GetUserCredentialCollection(string appId)
{
List<string> credentialList = new List<string>();
SecureStoreProvider prov = new SecureStoreProvider();
SPServiceContext context = SPServiceContext.GetContext(SPContext.Current.Site);
prov.Context = context; //當前上下文信息,以便從上下文中找到當前登陸用戶
try
{
SecureStoreCredentialCollection cc = prov.GetCredentials(appId);
for (int i = 0; i < cc.Count; i++)
{
ISecureStoreCredential c = cc[i];
IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(c.Credential);
string sDecrypString = System.Runtime.InteropServices.Marshal.PtrToStringUni(ptr);
credentialList.Add(sDecrypString);
}
}
catch
{
}
return credentialList;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> userInfoList =null;
try
{
userInfoList = GetUserCredentialCollection(this.AppName);
}
catch (Exception)
{
this.LabMsg.Text += "用戶憑據未設置,請在管理中心中設置!";
}
if (userInfoList.Count >= 2)
{
this.UserName.Value = userInfoList[0];
this.PassWord.Value = userInfoList[1];
_isHaveSSO = "true";
}
else
{
_isHaveSSO = "false";
}
}
}
