Beispiel #1
0
        public void MyTestMethodReadXML()
        {
            var xml = @"<cas:serviceResponse xmlns:cas=""http://www.yale.edu/tp/cas"">
  <cas:authenticationSuccess>
    <cas:user>[email protected]</cas:user>
    <cas:ext>
      <cas:Uid>1</cas:Uid>
      <cas:Sex>1</cas:Sex>
      <cas:NickName>zbw911</cas:NickName>
      <cas:City>170300</cas:City>
      <cas:Province>170000</cas:Province>
      <cas:UserName>[email protected]</cas:UserName>
    </cas:ext>
  </cas:authenticationSuccess>
</cas:serviceResponse>
";


            var xmlh = new XmlHelper();
            xmlh.LoadXML(xml, XmlHelper.LoadType.FromString);

            if (xmlh.RootNode.FirstChild.LocalName == "authenticationSuccess")
            {
                var value = xmlh.GetChildElementValue(xmlh.RootNode.FirstChild, "cas:user");
                Console.WriteLine(value);

                var exts = xmlh.GetFirstChildXmlNode(xmlh.RootNode.FirstChild, "cas:ext");


                var dic = new Dictionary<string, string>();


                for (var i = 0; i < exts.ChildNodes.Count; i++)
                {
                    var ext = exts.ChildNodes.Item(i);

                    dic.Add(ext.LocalName, ext.InnerText);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        ///   登录,如果这是一个登录的方法,那就直接去调用登录了
        /// </summary>
        /// <param name="strTicket"> </param>
        /// <param name="strService"> </param>
        /// <param name="strRedirectUrl"> </param>
        /// <param name="strUserName"> </param>
        /// <param name="strErrorText"> </param>
        /// <returns> </returns>
        public bool Login(
            string strTicket,
            string strService,
            out string strRedirectUrl,
            out string strUserName,
            out string strErrorText)
        {
            strRedirectUrl = "";
            strUserName = "";
            strErrorText = "";

            //如果没有登录成功就跳转到
            if (String.IsNullOrEmpty(strTicket))
            {
                strRedirectUrl = BuildLoginRequest(StrCasServerUrl, strService);

                return true;
            }

            // when we have a ticket, then validate it
            var strValidateUrl = BuildServiceValidateRequest(StrCasServerUrl, strService, strTicket);

            var isOK = false;
            try
            {
                //var xml = Dev.Comm.Net.Http.GetUrl(strValidateUrl);

                var xmlh = new XmlHelper();
                xmlh.LoadXML(strValidateUrl, XmlHelper.LoadType.FromURL);

                if (xmlh.RootNode.FirstChild.LocalName == "authenticationFailure")
                {
                    strErrorText = xmlh.RootNode.FirstChild.InnerText;
                }
                else if (xmlh.RootNode.FirstChild.LocalName == "authenticationSuccess")
                {
                    strUserName = xmlh.GetChildElementValue(xmlh.RootNode.FirstChild, "cas:user");

                    //ext Infos
                    var exts = xmlh.GetFirstChildXmlNode(xmlh.RootNode.FirstChild, "cas:ext");
                    var dic = new Dictionary<string, string>();

                    if (exts != null)
                    {
                        for (var i = 0; i < exts.ChildNodes.Count; i++)
                        {
                            var ext = exts.ChildNodes.Item(i);

                            dic.Add(ext.LocalName, ext.InnerText);
                        }
                    }

                    //hand User
                    UserAuthenticateManager.Provider.SignUserLogin(strUserName, extDatas: dic);

                    User.UserInfo.SetCurrentUserName(strUserName);

                    isOK = true;
                }
            }
            catch (Exception e)
            {
                strErrorText = e.Message;

                Dev.Log.Loger.Error(e);
            }

            return isOK;
        }