コード例 #1
0
        /// <summary>An IHttpRequest extension method that removes the session described by httpReq.</summary>
        ///
        /// <param name="httpReq">The httpReq to act on.</param>
        public static void RemoveSession(this IHttpRequest httpReq)
        {
            if (httpReq == null)
            {
                return;
            }

            using (var cache = httpReq.GetCacheClient())
            {
                var sessionKey = SessionFeature.GetSessionKey(httpReq.GetSessionId());
                cache.Remove(sessionKey);
            }

            httpReq.Items.Remove(RequestItemsSessionKey);
        }
コード例 #2
0
        /// <summary>An IHttpRequest extension method that saves a session.</summary>
        ///
        /// <param name="httpReq">  The httpReq to act on.</param>
        /// <param name="session">  The session.</param>
        /// <param name="expiresIn">The expires in.</param>
        public static void SaveSession(this IHttpRequest httpReq, IAuthSession session, TimeSpan?expiresIn = null)
        {
            if (httpReq == null)
            {
                return;
            }

            using (var cache = httpReq.GetCacheClient())
            {
                var sessionKey = SessionFeature.GetSessionKey(httpReq.GetSessionId());
                cache.CacheSet(sessionKey, session, expiresIn ?? AuthFeature.GetDefaultSessionExpiry());
            }

            httpReq.Items[RequestItemsSessionKey] = session;
        }
コード例 #3
0
        /// <summary>Authenticate if basic authentication.</summary>
        ///
        /// <param name="req">The request.</param>
        /// <param name="res">The resource.</param>
        public static void AuthenticateIfBasicAuth(IHttpRequest req, IHttpResponse res)
        {
            //Need to run SessionFeature filter since its not executed before this attribute (Priority -100)
            SessionFeature.AddSessionIdToRequestFilter(req, res, null); //Required to get req.GetSessionId()

            var userPass = req.GetBasicAuthUserAndPassword();

            if (userPass != null)
            {
                var authService = req.TryResolve <AuthService>();
                authService.RequestContext = new HttpRequestContext(req, res, null);
                var response = authService.Post(new Auth.Auth {
                    provider = BasicAuthProvider.Name,
                    UserName = userPass.Value.Key,
                    Password = userPass.Value.Value
                });
            }
        }
コード例 #4
0
        /// <summary>An IHttpRequest extension method that gets a session.</summary>
        ///
        /// <param name="httpReq">The httpReq to act on.</param>
        /// <param name="reload"> true to reload.</param>
        ///
        /// <returns>The session.</returns>
        public static IAuthSession GetSession(this IHttpRequest httpReq, bool reload = false)
        {
            if (httpReq == null)
            {
                return(null);
            }

            object oSession = null;

            if (!reload)
            {
                httpReq.Items.TryGetValue(RequestItemsSessionKey, out oSession);
            }

            if (oSession != null)
            {
                return((IAuthSession)oSession);
            }

            using (var cache = httpReq.GetCacheClient())
            {
                var sessionId = httpReq.GetSessionId();
                var session   = cache.Get <IAuthSession>(SessionFeature.GetSessionKey(sessionId));
                if (session == null)
                {
                    session           = AuthService.CurrentSessionFactory();
                    session.Id        = sessionId;
                    session.CreatedAt = session.LastModified = DateTime.UtcNow;
                    session.OnCreated(httpReq);
                }

                if (httpReq.Items.ContainsKey(RequestItemsSessionKey))
                {
                    httpReq.Items.Remove(RequestItemsSessionKey);
                }

                httpReq.Items.Add(RequestItemsSessionKey, session);
                return(session);
            }
        }
コード例 #5
0
        /// <summary>An ICacheClient extension method that session as.</summary>
        ///
        /// <typeparam name="TUserSession">Type of the user session.</typeparam>
        /// <param name="cache">  The cache to act on.</param>
        /// <param name="httpReq">The HTTP request.</param>
        /// <param name="httpRes">The HTTP resource.</param>
        ///
        /// <returns>A TUserSession.</returns>
        public static TUserSession SessionAs <TUserSession>(this ICacheClient cache,
                                                            IHttpRequest httpReq = null, IHttpResponse httpRes = null)
        {
            var sessionKey = GetSessionKey(httpReq);

            if (sessionKey != null)
            {
                var userSession = cache.Get <TUserSession>(sessionKey);
                if (!Equals(userSession, default(TUserSession)))
                {
                    return(userSession);
                }
            }

            if (sessionKey == null)
            {
                SessionFeature.CreateSessionIds(httpReq, httpRes);
            }

            var unAuthorizedSession = (TUserSession)typeof(TUserSession).CreateInstance();

            return(unAuthorizedSession);
        }
コード例 #6
0
        /// <summary>Authenticate if digest authentication.</summary>
        ///
        /// <param name="req">The request.</param>
        /// <param name="res">The resource.</param>
        public static void AuthenticateIfDigestAuth(IHttpRequest req, IHttpResponse res)
        {
            //Need to run SessionFeature filter since its not executed before this attribute (Priority -100)
            SessionFeature.AddSessionIdToRequestFilter(req, res, null); //Required to get req.GetSessionId()

            var digestAuth = req.GetDigestAuth();

            if (digestAuth != null)
            {
                var authService = req.TryResolve <AuthService>();
                authService.RequestContext = new HttpRequestContext(req, res, null);
                var response = authService.Post(new Auth.Auth
                {
                    provider = DigestAuthProvider.Name,
                    nonce    = digestAuth["nonce"],
                    uri      = digestAuth["uri"],
                    response = digestAuth["response"],
                    qop      = digestAuth["qop"],
                    nc       = digestAuth["nc"],
                    cnonce   = digestAuth["cnonce"],
                    UserName = digestAuth["username"]
                });
            }
        }
コード例 #7
0
        /// <summary>Gets session key.</summary>
        ///
        /// <param name="httpReq">The HTTP request.</param>
        ///
        /// <returns>The session key.</returns>
        public static string GetSessionKey(IHttpRequest httpReq = null)
        {
            var sessionId = SessionFeature.GetSessionId(httpReq);

            return(sessionId == null ? null : SessionFeature.GetSessionKey(sessionId));
        }