コード例 #1
0
        /// <summary>
        /// Creates a response that sets the authentication cookie and redirects
        /// the user back to where they came from.
        /// </summary>
        /// <param name="context">Current context</param>
        /// <param name="userIdentifier">User identifier guid</param>
        /// <param name="cookieExpiry">Optional expiry date for the cookie (for 'Remember me')</param>
        /// <param name="fallbackRedirectUrl">Url to redirect to if none in the querystring</param>
        /// <returns>Nancy response with redirect.</returns>
        public static Response UserLoggedInRedirectResponse(NancyContext context, Guid userIdentifier, DateTime?cookieExpiry = null, string fallbackRedirectUrl = null)
        {
            var redirectUrl = fallbackRedirectUrl;

            if (string.IsNullOrEmpty(redirectUrl))
            {
                redirectUrl = context.Request.Url.BasePath;
            }

            if (string.IsNullOrEmpty(redirectUrl))
            {
                redirectUrl = "/";
            }

            string redirectQuerystringKey = GetRedirectQuerystringKey(currentConfiguration);

            if (context.Request.Query[redirectQuerystringKey].HasValue)
            {
                var queryUrl = (string)context.Request.Query[redirectQuerystringKey];

                if (context.IsLocalUrl(queryUrl))
                {
                    redirectUrl = queryUrl;
                }
            }

            var response             = context.GetRedirect(redirectUrl);
            var authenticationCookie = BuildCookie(userIdentifier, cookieExpiry, currentConfiguration);

            response.AddCookie(authenticationCookie);

            return(response);
        }
コード例 #2
0
ファイル: FormsAuthentication.cs プロジェクト: ktairov/Nancy
        /// <summary>
        /// Logs the user out and redirects them to a URL
        /// </summary>
        /// <param name="context">Current context</param>
        /// <param name="redirectUrl">URL to redirect to</param>
        /// <returns>Nancy response</returns>
        public static Response LogOutAndRedirectResponse(NancyContext context, string redirectUrl)
        {
            var response = context.GetRedirect(redirectUrl);
            var authenticationCookie = BuildLogoutCookie(currentConfiguration);
            response.AddCookie(authenticationCookie);

            return response;
        }
コード例 #3
0
        /// <summary>
        /// Logs the user out and redirects them to a URL
        /// </summary>
        /// <param name="context">Current context</param>
        /// <param name="redirectUrl">URL to redirect to</param>
        /// <returns>Nancy response</returns>
        public static Response LogOutAndRedirectResponse(NancyContext context, string redirectUrl)
        {
            var response             = context.GetRedirect(redirectUrl);
            var authenticationCookie = BuildLogoutCookie(currentConfiguration);

            response.AddCookie(authenticationCookie);

            return(response);
        }
コード例 #4
0
        /// <summary>
        /// Creates a response that sets the authentication cookie and redirects
        /// the user back to where they came from.
        /// </summary>
        /// <param name="context">Current context</param>
        /// <param name="userIdentifier">User identifier guid</param>
        /// <param name="cookieExpiry">Optional expiry date for the cookie (for 'Remember me')</param>
        /// <param name="fallbackRedirectUrl">Url to redirect to if none in the querystring</param>
        /// <returns>Nancy response with redirect.</returns>
        public static Response UserLoggedInRedirectResponse(NancyContext context, Guid userIdentifier, DateTime?cookieExpiry = null, string fallbackRedirectUrl = "/")
        {
            var redirectUrl = fallbackRedirectUrl;

            var response             = context.GetRedirect(redirectUrl);
            var authenticationCookie = BuildCookie(userIdentifier, cookieExpiry, currentConfiguration);

            response.AddCookie(authenticationCookie);

            return(response);
        }
コード例 #5
0
ファイル: SecureModule.cs プロジェクト: xiaopohou/MZBlog
        private Response SetCurrentUserToViewBag(NancyContext ctx)
        {
            var author = _viewProjectionFactory.Get <string, Author>(ctx.CurrentUser.UserName);

            if (author == null)
            {
                return(ctx.GetRedirect("/mz-login?returnUrl=" + Request.Url.Path).AddCookie(FormsAuthentication.CreateLogoutCookie()));
            }

            ViewBag.CurrentUser = author;
            return(null);
        }
コード例 #6
0
        private Response SetContextUserFromAuthenticationCookie(NancyContext ctx)
        {
            var username = FormsAuthentication.GetAuthUsernameFromCookie(ctx);

            if (username.IsNullOrWhitespace())
            {
                return(ctx.GetRedirect("/session/login?returnUrl=" + Request.Url.Path));
            }

            ctx.CurrentUser = new UserIdentityWrapper(username, new string[] {});

            return(null);
        }
コード例 #7
0
ファイル: SecureModule.cs プロジェクト: xiaopohou/MZBlog
        private Response SetContextUserFromAuthenticationCookie(NancyContext ctx)
        {
            var username = FormsAuthentication.GetAuthUsernameFromCookie(ctx);

            if (username.IsNullOrWhitespace())
            {
                return(ctx.GetRedirect("/mz-login?returnUrl=" + Request.Url.Path).AddCookie(FormsAuthentication.CreateLogoutCookie()));
            }

            ctx.CurrentUser = new BlogUserIdentity(username, new string[] { "admin" });

            return(null);
        }
コード例 #8
0
        public static Response DoAlipayBrowserOnlyOption(NancyContext context, AlipayBrowserOnlyOptions options)
        {
            if (!string.IsNullOrWhiteSpace(options.RedirectUrl))
            {
                return(context.GetRedirect(options.RedirectUrl));
            }

            Response response = options.Message;

            response.StatusCode   = options.StatusCode;
            response.ReasonPhrase = options.Message;
            return(response);
        }
コード例 #9
0
ファイル: FormsAuthentication.cs プロジェクト: rmueller/Nancy
        /// <summary>
        /// Creates a response that sets the authentication cookie and redirects
        /// the user back to where they came from.
        /// </summary>
        /// <param name="context">Current context</param>
        /// <param name="userIdentifier">User identifier guid</param>
        /// <param name="cookieExpiry">Optional expiry date for the cookie (for 'Remember me')</param>
        /// <param name="fallbackRedirectUrl">Url to redirect to if none in the querystring</param>
        /// <returns>Nancy response with redirect.</returns>
        public static Response UserLoggedInRedirectResponse(NancyContext context, Guid userIdentifier, DateTime?cookieExpiry = null, string fallbackRedirectUrl = "/")
        {
            var redirectUrl = fallbackRedirectUrl;

            if (context.Request.Query[REDIRECT_QUERYSTRING_KEY].HasValue)
            {
                redirectUrl = context.Request.Query[REDIRECT_QUERYSTRING_KEY];
            }

            var response             = context.GetRedirect(redirectUrl);
            var authenticationCookie = BuildCookie(userIdentifier, cookieExpiry, currentConfiguration);

            response.AddCookie(authenticationCookie);

            return(response);
        }
コード例 #10
0
        /// <summary>
        /// Logs the user out and redirects them to a URL
        /// </summary>
        /// <param name="context">
        /// Current context
        /// </param>
        /// <param name="redirectUrl">
        /// URL to redirect to
        /// </param>
        /// <returns>
        /// Nancy response
        /// </returns>
        public Response LogOutAndRedirectResponse(NancyContext context, string redirectUrl)
        {
            var response = context.GetRedirect(redirectUrl);
            var userName = context.CurrentUser != null
                               ? context.CurrentUser.UserName
                               : string.Empty;

            if (!string.IsNullOrWhiteSpace(userName))
            {
                // remove the existing cookie
                if (this.EvictCredentialFromCache(userName))
                {
                    Logger.Info("User '{0}' has logged out", userName);
                }
            }

            var authenticationCookie = this.BuildLogoutCookie(this.currentConfiguration);

            response.WithCookie(authenticationCookie);

            return(response);
        }
コード例 #11
0
ファイル: FormsAuthentication.cs プロジェクト: ktairov/Nancy
        /// <summary>
        /// Creates a response that sets the authentication cookie and redirects
        /// the user back to where they came from.
        /// </summary>
        /// <param name="context">Current context</param>
        /// <param name="userIdentifier">User identifier guid</param>
        /// <param name="cookieExpiry">Optional expiry date for the cookie (for 'Remember me')</param>
        /// <param name="fallbackRedirectUrl">Url to redirect to if none in the querystring</param>
        /// <returns>Nancy response with redirect.</returns>
        public static Response UserLoggedInRedirectResponse(NancyContext context, Guid userIdentifier, DateTime? cookieExpiry = null, string fallbackRedirectUrl = "/")
        {
            var redirectUrl = fallbackRedirectUrl;
            string redirectQuerystringKey = GetRedirectQuerystringKey(currentConfiguration);

            if (context.Request.Query[redirectQuerystringKey].HasValue)
            {
                redirectUrl = context.Request.Query[redirectQuerystringKey];
            }

            var response = context.GetRedirect(redirectUrl);
            var authenticationCookie = BuildCookie(userIdentifier, cookieExpiry, currentConfiguration);
            response.AddCookie(authenticationCookie);

            return response;
        }
コード例 #12
0
        /// <summary>
        /// Logs out the user from facebook and redirects to a given path (this will be normally the application logout path to complete the full logout)
        /// </summary>
        public static Response LogoutFromFacebookAndRedirect(NancyContext context, string path)
        {
            var facebookId = ApplicationAuthenticator.GetFacebookId(context);

            if (facebookId.HasValue)
            {
                var accessToken = FacebookCurrentAuthenticatedUserCache.GetAccessToken(facebookId.Value);

                return context.GetRedirect(FacebookOAuthService.GetFacebookLogoutUrl(path, accessToken));
            }

            return null;
        }
コード例 #13
0
        public static Response RedirectToFacebookLoginAndResetAuthenticationWhenNotAuthenticatedByFacebook(NancyContext context)
        {
            if (Enabled && !IsAuthenticatedByFacebook(context))
            {

                RemoveUserFromCache(context);
                ApplicationAuthenticator.SetAsNotAuthenticated(context);
                return context.GetRedirect(Configuration.FacebookLoginPath);

            }
            return context.Response;
        }
コード例 #14
0
        /// <summary>
        /// Creates a response that sets the authentication cookie and redirects
        /// the user back to where they came from.
        /// </summary>
        /// <param name="context">Current context</param>
        /// <param name="userIdentifier">User identifier guid</param>
        /// <param name="cookieExpiry">Optional expiry date for the cookie (for 'Remember me')</param>
        /// <param name="fallbackRedirectUrl">Url to redirect to if none in the querystring</param>
        /// <returns>Nancy response with redirect.</returns>
        public static Response UserLoggedInRedirectResponse(NancyContext context, Guid userIdentifier, DateTime? cookieExpiry = null, string fallbackRedirectUrl = null)
        {
            var redirectUrl = fallbackRedirectUrl;

            if (string.IsNullOrEmpty(redirectUrl))
            {
                redirectUrl = context.Request.Url.BasePath;
            }

            if (string.IsNullOrEmpty(redirectUrl))
            {
                redirectUrl = "/";
            }

            string redirectQuerystringKey = GetRedirectQuerystringKey(currentConfiguration);

            if (context.Request.Query[redirectQuerystringKey].HasValue)
            {
                var queryUrl = (string)context.Request.Query[redirectQuerystringKey];

                if (context.IsLocalUrl(queryUrl))
                {
                    redirectUrl = queryUrl;
                }
            }

            var response = context.GetRedirect(redirectUrl);
            var authenticationCookie = BuildCookie(userIdentifier, cookieExpiry, currentConfiguration);
            response.WithCookie(authenticationCookie);

            return response;
        }
コード例 #15
0
        public static Response LoginIntoApplicationWithFacebookOAthResponse(NancyContext context, string pathToRedirectOnAutheticationFailure)
        {
            string code = context.Request.Query.code;
            if (FacebookOAuthService.IsOAthResultSuccess(context))
            {
                var accessToken = FacebookOAuthService.GetAccessToken(code);
                var me = FacebookClientService.GetFacebookMe(accessToken);
                AddAuthenticatedUserToCache(me, accessToken);
                var facebookId = Convert.ToInt64(me.id);
                return LoginIntoTheApplicationAndRedirect(context, facebookId);
            }

            return context.GetRedirect(pathToRedirectOnAutheticationFailure);
        }
コード例 #16
0
 public static Response RedirectToFacebookLoginUrl(NancyContext context)
 {
     //TODO: The login parameters should be configurable
     //TODO: Extract the redirect url (from the application authenticator) and pass it as a parameter
     return context.GetRedirect(FacebookOAuthService.GetAbsoluteLoginUrl(Configuration.FacebookExtendedPermissions));
 }
コード例 #17
0
        /// <summary>
        ///     Logs the user out and redirects them to a URL
        /// </summary>
        /// <param name="context">Current context</param>
        /// <param name="redirectUrl">URL to redirect to</param>
        /// <returns>Nancy response</returns>
        public static Response LogOutAndRedirectResponse(NancyContext context, string redirectUrl)
        {
            var response = context.GetRedirect(redirectUrl);

            var authenticationCookie = BuildLogoutCookie(currentConfiguration);

            response.WithCookie(authenticationCookie);

            context.Items[UserLoggedOutKey] = new object();
            context.CurrentUser = null;

            if (context.Items.ContainsKey(AuthSessionIdItemKey))
            {
                currentConfiguration.AuthSessionIdStore.Remove((Guid)context.Items[AuthSessionIdItemKey]);
            }

            return response;
        }