コード例 #1
0
 protected void CheckPermission()
 {
     if (!SecurityContext.IsAuthenticated)
     {
         try
         {
             if (!SecurityContext.AuthenticateMe(CookiesManager.GetAuthCookie()))
             {
                 throw GenerateException(HttpStatusCode.Unauthorized, "Unauthorized", null);
             }
         }
         catch (Exception exception)
         {
             throw GenerateException(HttpStatusCode.Unauthorized, "Unauthorized", exception);
         }
     }
 }
コード例 #2
0
        public bool IsValidUserKey(RequestContext requestContext)
        {
            if (requestContext != null && requestContext.RequestMessage != null)
            {
                var prop   = (HttpRequestMessageProperty)requestContext.RequestMessage.Properties[HttpRequestMessageProperty.Name];
                var cookie = prop.Headers[HttpRequestHeader.Cookie];
                if (string.IsNullOrEmpty(cookie))
                {
                    return(false);
                }

                var coockieName = CookiesManager.GetCookiesName();
                foreach (string s in cookie.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (s.IndexOf('=') == -1)
                    {
                        continue;
                    }

                    var key = s.Substring(0, s.IndexOf('=')).Trim();
                    if (key.Equals(coockieName, StringComparison.Ordinal))
                    {
                        try
                        {
                            var value = s.Substring(s.IndexOf('=') + 1).Trim();
                            if (SecurityContext.IsAuthenticated || SecurityContext.AuthenticateMe(value))
                            {
                                return(true);
                            }
                        }
                        catch { }
                        break;
                    }
                }
            }
            return(false);
        }