SharePoint2010单点登录
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";}}}