Ejemplo n.º 1
0
        private bool GetBasicAuthUserContext(HttpListenerRequest request, out HttpUserContext userContext)
        {
            var authHeader = request.Headers.Get("Authorization");

            if (authHeader != null)
            {
                if (authHeader.StartsWith("Basic ", StringComparison.OrdinalIgnoreCase))
                {
                    var encodedAuth = authHeader.Substring(6).Trim();
                    var authBytes   = Convert.FromBase64String(encodedAuth);
                    var authKey     = Encoding.UTF8.GetString(authBytes);

                    var i = authKey.IndexOf(':');
                    if (i >= 0)
                    {
                        var userCreds = new HttpUserCredentials {
                            Username = authKey.Substring(0, i),
                            Password = authKey.Substring(i + 1),
                        };

                        userContext = Authorization.Authorize(userCreds);
                        return(userContext != null);
                    }
                }
            }

            userContext = null;
            return(false);
        }
Ejemplo n.º 2
0
        private bool GetCookieUserContext(HttpListenerRequest request, out HttpUserContext userContext)
        {
            var authCookie = request.Cookies[CookieName];
            var token      = authCookie?.Value;

            if (!string.IsNullOrEmpty(token) && userTokens.TryGet(token, out userContext))
            {
                return(true);
            }

            userContext = null;
            return(false);
        }
Ejemplo n.º 3
0
        public bool TryGetUserContext(HttpListenerRequest request, out HttpUserContext userContext)
        {
            if (GetCookieUserContext(request, out userContext))
            {
                userContext.Restart();
                return(true);
            }

            if (GetBasicAuthUserContext(request, out userContext))
            {
                userContext.Restart();
                return(true);
            }

            userContext = null;
            return(false);
        }