MOSS2010单点登录(2)

来源:互联网 发布:程序员宣传片百度网盘 编辑:程序博客网 时间:2024/06/06 03:56

 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";                }            }        }



 

原创粉丝点击