示例#1
0
 private void SerializeMRX()
 {
     this.lst = new List <Cookie2>();
     if (CookieString.Trim() != "")
     {
         /// modes
         /// cokname
         /// cokname=cokval
         /// cokname1=cokval1 ; cokname2=cokval2
         if (!CookieString.Contains('=') && !CookieString.Contains(';'))
         {
             lst.Add(new Cookie2(CookieString, ""));
         }
         else if (CookieString.Contains('=') && !CookieString.Contains(';'))
         {
             string[] candv = CookieString.Split(new char[] { '=' });
             lst.Add(new Cookie2(candv[0], candv[1]));
         }
         else if (CookieString.Contains('=') && CookieString.Contains(';'))
         {
             string[] coks = CookieString.Split(new char[] { ';' });
             foreach (string pair in coks)
             {
                 /// modes
                 /// cokname
                 /// cokname=cokval
                 if (!pair.Contains('='))
                 {
                     lst.Add(new Cookie2(pair, ""));
                 }
                 else if (pair.Contains('='))
                 {
                     string[] c_and_v = pair.Split(new char[] { '=' });
                     if (c_and_v[0].Trim() != "")
                     {
                         lst.Add(new Cookie2(c_and_v[0], c_and_v[1]));
                     }
                 }
             }
         }
     }
 }
示例#2
0
        public static WinInetCredentialsContext GetCredentialsContext(IBlogClient blogClient, IBlogCredentialsAccessor credentials, string url)
        {
            // determine cookies and/or network credentials
            CookieString      cookieString = null;
            NetworkCredential credential   = null;

            if (credentials != null && credentials.Username != String.Empty)
            {
                credential = new NetworkCredential(credentials.Username, credentials.Password);
            }

            if (cookieString != null || credential != null)
            {
                return(new WinInetCredentialsContext(credential, cookieString));
            }
            else
            {
                return(null);
            }
        }
示例#3
0
        /// <summary>
        /// Method to check user authentication on server.
        /// </summary>
        /// <returns>true if user is authenticate otherwise false.</returns>
        protected bool IsAuth()
        {
            log.Info("Api : Checking user authentication on server. Please wait !");

            try
            {
                // Try to found sid cookie.
                // sid cookie contains at base 2 cookies but a big bug on Cookies sender append all cookies in a same string
                // so we use only 1 string as cookies container.
                // todo : Correct bug sender or rewrite all Http Server base with another solution more viable.
                string[] cook = new string[] { "", "" };

                CookieCollection cookies = Uri.Cookies;
                if (cookies != null && cookies.Count > 0)
                {
                    foreach (Cookie cookie in Uri.Cookies)
                    {
                        if (cookie.Path == "" || cookie.Path == "/api/user")
                        {
                            if (cookie.Name == "sid")
                            {
                                CookieString = cookie.Value;
                                cook         = CookieString.Split(new char[] { ':' });
                            }
                        }
                    }
                }
                else
                {
                    try
                    {
                        var a = Uri;
                        CookieString = Uri.QueryString["sid"];
                        cook         = CookieString.Split(new char[] { ':' });
                    }
                    catch {}
                }
                string sid    = cook[0];
                string stoken = cook[1];

                // todo : Alternative code with query params, use it if bug was corrected :
                // string sid = Uri.QueryString["sid"];
                // string stoken = Uri.QueryString["stoken"];

                // Get user token stored in the session.
                string token = (string)HttpWebServerApplication.Session.Get(sid, "sid");
                log.Debug(string.Format("Api : Authentication receive sid={0}, stoken={1}", sid, stoken));
                log.Debug(string.Format("Api : Authentication in session token={0}", token));

                if (token == null || token == "" || token != stoken)
                {
                    log.Info("Api : Checking user authentication on server. Unauthorized !");
                    return(false);
                }

                CookAuth["sid"]   = sid;
                CookAuth["token"] = stoken;

                log.Info("Api : Checking user authentication on server. Authorized !");
                return(true);
            }
            catch (Exception e)
            {
                log.Error("Error : User authentication to server failed.");
                log.Error(string.Format("Error : {0}", e.Message));
                log.Error(e.StackTrace);

                return(false);
            }
        }