コード例 #1
0
        public async Task CanSpecifyAndShareDataProtector()
        {
            var dp      = new NoOpDataProtector();
            var server1 = TestServer.Create(app =>
            {
                app.UseCookieAuthentication(options =>
                {
                    options.TicketDataFormat = new TicketDataFormat(dp);
                    options.CookieName       = "Cookie";
                });
                app.Use((context, next) =>
                        context.Authentication.SignInAsync("Cookies",
                                                           new ClaimsPrincipal(new ClaimsIdentity(new GenericIdentity("Alice", "Cookies"))),
                                                           new AuthenticationProperties()));
            },
                                            services => services.AddAuthentication());

            var transaction = await SendAsync(server1, "http://example.com/stuff");

            Assert.NotNull(transaction.SetCookie);

            var server2 = TestServer.Create(app =>
            {
                app.UseCookieAuthentication(options =>
                {
                    options.AuthenticationScheme = "Cookies";
                    options.CookieName           = "Cookie";
                    options.TicketDataFormat     = new TicketDataFormat(dp);
                });
                app.Use(async(context, next) =>
                {
                    var authContext = new AuthenticateContext("Cookies");
                    await context.Authentication.AuthenticateAsync(authContext);
                    Describe(context.Response, authContext);
                });
            },
                                            services => services.AddAuthentication());
            var transaction2 = await SendAsync(server2, "http://example.com/stuff", transaction.CookieNameValue);

            Assert.Equal("Alice", FindClaimValue(transaction2, ClaimTypes.Name));
        }
コード例 #2
0
        public async Task CanSpecifyAndShareDataProtector()
        {

            var dp = new NoOpDataProtector();
            var builder1 = new WebHostBuilder()
                .Configure(app =>
                {
                    app.UseCookieAuthentication(new CookieAuthenticationOptions
                    {
                        TicketDataFormat = new TicketDataFormat(dp),
                        CookieName = "Cookie"
                    });
                    app.Use((context, next) =>
                        context.Authentication.SignInAsync("Cookies",
                                        new ClaimsPrincipal(new ClaimsIdentity(new GenericIdentity("Alice", "Cookies"))),
                                        new AuthenticationProperties()));
                })
                .ConfigureServices(services => services.AddAuthentication());
            var server1 = new TestServer(builder1);

            var transaction = await SendAsync(server1, "http://example.com/stuff");
            Assert.NotNull(transaction.SetCookie);

            var builder2 = new WebHostBuilder()
                .Configure(app =>
                {
                    app.UseCookieAuthentication(new CookieAuthenticationOptions
                    {
                        AuthenticationScheme = "Cookies",
                        CookieName = "Cookie",
                        TicketDataFormat = new TicketDataFormat(dp)
                    });
                    app.Use(async (context, next) =>
                    {
                        var authContext = new AuthenticateContext("Cookies");
                        await context.Authentication.AuthenticateAsync(authContext);
                        Describe(context.Response, authContext);
                    });
                })
                .ConfigureServices(services => services.AddAuthentication());
            var server2 = new TestServer(builder2);
            var transaction2 = await SendAsync(server2, "http://example.com/stuff", transaction.CookieNameValue);
            Assert.Equal("Alice", FindClaimValue(transaction2, ClaimTypes.Name));
        }
コード例 #3
0
        public async Task CanSpecifyAndShareDataProtector()
        {

            var dp = new NoOpDataProtector();
            var server1 = TestServer.Create(app =>
            {
                app.UseCookieAuthentication(options =>
                {
                    options.TicketDataFormat = new TicketDataFormat(dp);
                    options.CookieName = "Cookie";
                });
                app.Use((context, next) =>
                    context.Authentication.SignInAsync("Cookies",
                                    new ClaimsPrincipal(new ClaimsIdentity(new GenericIdentity("Alice", "Cookies"))),
                                    new AuthenticationProperties()));
            },
            services => services.AddAuthentication());

            var transaction = await SendAsync(server1, "http://example.com/stuff");
            transaction.SetCookie.ShouldNotBe(null);

            var server2 = TestServer.Create(app =>
            {
                app.UseCookieAuthentication(options =>
                {
                    options.AuthenticationScheme = "Cookies";
                    options.CookieName = "Cookie";
                    options.TicketDataFormat = new TicketDataFormat(dp);
                });
                app.Use(async (context, next) =>
                {
                    var authContext = new AuthenticateContext("Cookies");
                    await context.Authentication.AuthenticateAsync(authContext);
                    Describe(context.Response, authContext);
                });
            },
            services => services.AddAuthentication());
            var transaction2 = await SendAsync(server2, "http://example.com/stuff", transaction.CookieNameValue);
            FindClaimValue(transaction2, ClaimTypes.Name).ShouldBe("Alice");
        }