Beispiel #1
0
        protected override void RequestStartup(ILifetimeScope container, IPipelines pipelines)
        {
            var formsAuthConfiguration =
                new FormsAuthenticationConfiguration
            {
                RedirectUrl = "~/login",
                UserMapper  = container.Resolve <IUserRepository>(),
            };

            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
        }
Beispiel #2
0
        private void SetupFormsAuthentication(IKernel container, IPipelines pipelines)
        {
            var formsAuthConfiguration =
                new FormsAuthenticationConfiguration
            {
                RedirectUrl = "~/account/login",
                UserMapper  = container.Get <IUserMapper>()
            };

            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
        }
Beispiel #3
0
        protected override void RequestStartup(TinyIoCContainer requestContainer, IPipelines pipelines, NancyContext context)
        {
            var formsAuthConfiguration =
                new FormsAuthenticationConfiguration()
            {
                RedirectUrl = "~/login",
                UserMapper  = requestContainer.Resolve <IUserMapper>(),
            };

            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
        }
        public void Should_redirect_to_given_url_if_local()
        {
            FormsAuthentication.Enable(A.Fake <IPipelines>(), this.config);
            context.Request.Query[config.RedirectQuerystringKey] = "~/login";

            var result = FormsAuthentication.UserLoggedInRedirectResponse(context, userGuid);

            result.ShouldBeOfType(typeof(Response));
            result.StatusCode.ShouldEqual(HttpStatusCode.SeeOther);
            result.Headers["Location"].ShouldEqual("/testing/login");
        }
        public void Should_have_authentication_cookie_in_login_response_when_logging_in_without_redirect()
        {
            // Given
            FormsAuthentication.Enable(A.Fake <IPipelines>(), this.config);

            // When
            var result = FormsAuthentication.UserLoggedInResponse(userGuid);

            // Then
            result.Cookies.Where(c => c.Name == FormsAuthentication.FormsAuthenticationCookieName).Any().ShouldBeTrue();
        }
        public void Should_add_a_pre_and_post_hook_when_enabled()
        {
            var pipelines = A.Fake <IPipelines>();

            FormsAuthentication.Enable(pipelines, this.config);

            A.CallTo(() => pipelines.BeforeRequest.AddItemToStartOfPipeline(A <Func <NancyContext, Response> > .Ignored))
            .MustHaveHappened(Repeated.Exactly.Once);
            A.CallTo(() => pipelines.AfterRequest.AddItemToEndOfPipeline(A <Action <NancyContext> > .Ignored))
            .MustHaveHappened(Repeated.Exactly.Once);
        }
Beispiel #7
0
        private void EnableFormAuth(IUnityContainer container, IPipelines pipelines)
        {
            var formsAuthConfiguration =
                new FormsAuthenticationConfiguration()
            {
                RedirectUrl = "~/",
                UserMapper  = container.Resolve <IUserMapper>(),
            };

            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
        }
        public void Should_redirect_to_base_path_if_non_local_url_and_no_fallback()
        {
            FormsAuthentication.Enable(A.Fake <IPipelines>(), this.config);
            context.Request.Query[config.RedirectQuerystringKey] = "http://moo.com/";

            var result = FormsAuthentication.UserLoggedInRedirectResponse(context, userGuid);

            result.ShouldBeOfType(typeof(Response));
            result.StatusCode.ShouldEqual(HttpStatusCode.SeeOther);
            result.Headers["Location"].ShouldEqual("/testing");
        }
Beispiel #9
0
        public void Should_return_ok_response_when_user_logs_out_without_redirect()
        {
            // Given
            FormsAuthentication.Enable(A.Fake <IPipelines>(), this.config);

            // When
            var result = FormsAuthentication.LogOutResponse();

            // Then
            result.ShouldBeOfType(typeof(Response));
            result.StatusCode.ShouldEqual(HttpStatusCode.OK);
        }
Beispiel #10
0
        protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);
            //form authentication
            var formsAuthConfiguration = new FormsAuthenticationConfiguration
            {
                RedirectUrl = ConfigHelper.GetAppSettingByKey("logonUrl"),
                UserMapper  = container.Resolve <IUserMapper>(),
            };

            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
        }
Beispiel #11
0
        protected override void RequestStartup(TinyIoC.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
        {
            var formsAuthConfiguration =
                new FormsAuthenticationConfiguration
            {
                RedirectUrl = "~/login",
                UserMapper  = container.Resolve <IUserMapper>(),
            };

            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
            base.RequestStartup(container, pipelines);
        }
Beispiel #12
0
        public void Should_encrypt_cookie()
        {
            var mockEncrypter = A.Fake <IEncryptionProvider>();

            this.config.EncryptionProvider = mockEncrypter;
            FormsAuthentication.Enable(A.Fake <IApplicationPipelines>(), this.config);

            FormsAuthentication.UserLoggedInRedirectResponse(context, userGuid, DateTime.Now.AddDays(1));

            A.CallTo(() => mockEncrypter.Encrypt(A <string> .Ignored, A <string> .Ignored, A <byte[]> .Ignored))
            .MustHaveHappened(Repeated.Exactly.Once);
        }
Beispiel #13
0
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines);
            var formsAuthConfiguration =
                new FormsAuthenticationConfiguration()
            {
                RedirectUrl = "~/login",
                UserMapper  = container.Resolve <IUserMapper>()
            };

            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
        }
Beispiel #14
0
        public void Should_add_a_pre_hook_but_not_a_post_hook_when_DisableRedirect_is_true()
        {
            var pipelines = A.Fake <IPipelines>();

            this.config.DisableRedirect = true;
            FormsAuthentication.Enable(pipelines, this.config);

            A.CallTo(() => pipelines.BeforeRequest.AddItemToStartOfPipeline(A <Func <NancyContext, Response> > .Ignored))
            .MustHaveHappenedOnceExactly();
            A.CallTo(() => pipelines.AfterRequest.AddItemToEndOfPipeline(A <Action <NancyContext> > .Ignored))
            .MustNotHaveHappened();
        }
Beispiel #15
0
        public void Should_encrypt_cookie_when_logging_in_with_redirect()
        {
            var mockEncrypter = A.Fake <IEncryptionProvider>();

            this.config.CryptographyConfiguration = new CryptographyConfiguration(mockEncrypter, this.cryptographyConfiguration.HmacProvider);
            FormsAuthentication.Enable(A.Fake <IPipelines>(), this.config);

            FormsAuthentication.UserLoggedInRedirectResponse(context, userGuid, DateTime.Now.AddDays(1));

            A.CallTo(() => mockEncrypter.Encrypt(A <string> .Ignored))
            .MustHaveHappened(Repeated.Exactly.Once);
        }
Beispiel #16
0
        public void Should_have_expired_empty_authentication_cookie_in_logout_response_when_user_logs_out_with_redirect()
        {
            FormsAuthentication.Enable(A.Fake <IPipelines>(), this.config);

            var result = FormsAuthentication.LogOutAndRedirectResponse(context, "/");

            var cookie = result.Cookies.Where(c => c.Name == FormsAuthentication.FormsAuthenticationCookieName).First();

            cookie.Value.ShouldBeEmpty();
            cookie.Expires.ShouldNotBeNull();
            (cookie.Expires < DateTime.Now).ShouldBeTrue();
        }
Beispiel #17
0
        public void Should_set_authentication_cookie_to_httponly_when_logging_in_with_redirect()
        {
            //Given
            FormsAuthentication.Enable(A.Fake <IPipelines>(), this.config);

            //When
            var result = FormsAuthentication.UserLoggedInRedirectResponse(context, userGuid);

            //Then
            result.Cookies.Where(c => c.Name == FormsAuthentication.FormsAuthenticationCookieName).First()
            .HttpOnly.ShouldBeTrue();
        }
Beispiel #18
0
        public void Should_set_expiry_date_if_one_specified_when_logging_in_without_redirect()
        {
            // Given
            FormsAuthentication.Enable(A.Fake <IPipelines>(), this.config);

            // When
            var result = FormsAuthentication.UserLoggedInResponse(userGuid, DateTime.Now.AddDays(1));

            // Then
            result.Cookies.Where(c => c.Name == FormsAuthentication.FormsAuthenticationCookieName).First()
            .Expires.ShouldNotBeNull();
        }
Beispiel #19
0
        protected override void RequestStartup(ILifetimeScope container, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);
            var formsAuthConfiguration = new FormsAuthenticationConfiguration()
            {
                RedirectUrl = "~/login",
                UserMapper  = container.Resolve <IUserMapper>(),
            };

            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
            CookieBasedSessions.Enable(pipelines);
        }
Beispiel #20
0
        protected override void RequestStartup(ILifetimeScope container, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);

            var config = new FormsAuthenticationConfiguration
            {
                RedirectUrl = "~/login",
                UserMapper  = container.Resolve <IUserMapper>()
            };

            FormsAuthentication.Enable(pipelines, config);
        }
        protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);

            var formsAuthConfiguration = new FormsAuthenticationConfiguration
            {
                RedirectUrl = "~/account/login",
                UserMapper  = container.Resolve <IUserMapper>()
            };

            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
        }
Beispiel #22
0
        public void Should_set_authentication_cookie_to_secure_when_config_requires_ssl_and_user_logs_out_without_redirect()
        {
            // Given
            FormsAuthentication.Enable(A.Fake <IPipelines>(), this.secureConfig);

            // When
            var result = FormsAuthentication.LogOutResponse();

            // Then
            var cookie = result.Cookies.Where(c => c.Name == FormsAuthentication.FormsAuthenticationCookieName).First();

            cookie.Secure.ShouldBeTrue();
        }
Beispiel #23
0
        public void Initialize(IPipelines pipelines)
        {
            var formsAuthConfiguration =
                new FormsAuthenticationConfiguration()
            {
                RedirectUrl = "~/login",
                UserMapper  = Global.Sessions
            };

            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);

            pipelines.OnError += HandleException;
        }
Beispiel #24
0
        protected override void ApplicationStartup(ILifetimeScope container, IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines);

            ClientAppSettings.Enable(pipelines);
            Elmahlogging.Enable(pipelines, "elmah");
            CustomErrors.Enable(pipelines, new ErrorHandlingConfiguration());
            FormsAuthentication.Enable(pipelines, new FormsAuthenticationConfiguration
            {
                RedirectUrl = "~/login",
                UserMapper  = container.Resolve <IUserMapper>()
            });
        }
Beispiel #25
0
        protected override void InitialiseInternal(TinyIoC.TinyIoCContainer container)
        {
            base.InitialiseInternal(container);

            var formsAuthConfiguration =
                new FormsAuthenticationConfiguration()
            {
                RedirectUrl = "~/login",
                UserMapper  = container.Resolve <IUserMapper>(),
            };

            FormsAuthentication.Enable(this, formsAuthConfiguration);
        }
Beispiel #26
0
        protected override void RequestStartup(TinyIoCContainer requestContainer, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(requestContainer, pipelines, context);

            var config = new FormsAuthenticationConfiguration()
            {
                RedirectUrl = "~/login",
                UserMapper  = requestContainer.Resolve <IUserMapper>(),
                CryptographyConfiguration = _cryptographyConfiguration
            };

            FormsAuthentication.Enable(pipelines, config);
        }
        protected override void ApplicationStartup(TinyIoCContainer _container, IPipelines _pipelines)
        {
            CookieBasedSessions.Enable(_pipelines);
            Nancy.Security.Csrf.Enable(_pipelines);

            var formsAuthConfiguration = new FormsAuthenticationConfiguration()
            {
                RedirectUrl = "/login",
                UserMapper  = _container.Resolve <IUserMapper>(),
            };

            FormsAuthentication.Enable(_pipelines, formsAuthConfiguration);
        }
Beispiel #28
0
        protected override void InitialiseInternal(IContainer container)
        {
            base.InitialiseInternal(container);

            var formsAuthConfiguration =
                new FormsAuthenticationConfiguration()
            {
                RedirectUrl    = "~/login",
                UsernameMapper = container.GetInstance <IUsernameMapper>()
            };

            FormsAuthentication.Enable(this, formsAuthConfiguration);
        }
Beispiel #29
0
        public void Should_set_Path_when_config_provides_path_value()
        {
            //Given
            FormsAuthentication.Enable(A.Fake <IPipelines>(), this.domainPathConfig);

            //When
            var result = FormsAuthentication.UserLoggedInRedirectResponse(context, userGuid);

            //Then
            var cookie = result.Cookies.Where(c => c.Name == FormsAuthentication.FormsAuthenticationCookieName).First();

            cookie.Path.ShouldEqual(path);
        }
Beispiel #30
0
        // 每次请求都会触发,一个页面会触发多次
        protected override void RequestStartup(TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);

            // At request startup we modify the request pipelines to
            // include forms authentication - passing in our now request
            // scoped user name mapper.
            //
            // The pipelines passed in here are specific to this request,
            // so we can add/remove/update items in them as we please.
            var formsAuthConfiguration =
                new FormsAuthenticationConfiguration()
            {
                RedirectUrl = "~/account/logon",
                UserMapper  = container.Resolve <IUserMapper>(),
            };

            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);



            //log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            //pipelines.OnError.AddItemToEndOfPipeline((ctx, exception) => {
            //    Task tasks = new Task(() => {
            //        log.Error(exception.Message);
            //    });

            //    DefaultJsonSerializer serializer = new DefaultJsonSerializer();
            //    Response error = new JsonResponse(exception.Message, serializer);
            //    error.StatusCode = HttpStatusCode.InternalServerError;
            //    return error;
            //});



            // Enabling sessions in Nancy
            CookieBasedSessions.Enable(pipelines);

            //放RequestStartup这里是每次请求时判断session,为了避免session过期,所以不放在ApplicationStartup
            pipelines.BeforeRequest += (ctx) => {
                var uid  = ctx.Request.Session["TempUserId"];
                var user = ctx.CurrentUser;
                if (user == null && uid == null)
                {
                    //ctx.Request.Session["TempUserId"] = "temp-" + DateTime.Now.ToString("-yyyy-MM-dd-hh-mm-ss-fffff");
                    ctx.Request.Session["TempUserId"] = "temp-" + Guid.NewGuid().ToString();
                }
                return(null);
                //return <null or a Response object>;
            };
        }