public Task AuthenticateAsync(AuthenticateContext context)
        {
            context.Authenticated(
                ClaimsPrincipalBuilder.DefaultAuthenticated,
                new Dictionary<string, string>(),
                new Dictionary<string, object>());

            return TaskCache.CompletedTask;
        }
 public async Task AuthenticateAsync(AuthenticateContext context)
 {
     if (PriorHandler != null)
     {
         await PriorHandler.AuthenticateAsync(context);
         if (_transform != null && context?.Principal != null)
         {
             var transformationContext = new ClaimsTransformationContext(_httpContext)
             {
                 Principal = context.Principal
             };
             context.Authenticated(
                 await _transform.TransformAsync(transformationContext),
                 context.Properties,
                 context.Description);
         }
     }
 }
        public void AuthenticateContext_Authenticated()
        {
            // Arrange
            var context = new AuthenticateContext("test");

            var principal = new ClaimsPrincipal();
            var properties = new Dictionary<string, string>();
            var description = new Dictionary<string, object>();

            // Act
            context.Authenticated(principal, properties, description);

            // Assert
            Assert.True(context.Accepted);
            Assert.Equal("test", context.AuthenticationScheme);
            Assert.Same(description, context.Description);
            Assert.Null(context.Error);
            Assert.Same(principal, context.Principal);
            Assert.Same(properties, context.Properties);
        }
示例#4
0
        public void AuthenticateContext_Failed_SetsUnusedPropertiesToDefault()
        {
            // Arrange
            var context = new AuthenticateContext("test");

            var exception = new Exception();

            context.Authenticated(new ClaimsPrincipal(), new Dictionary <string, string>(), new Dictionary <string, object>());

            // Act
            context.Failed(exception);

            // Assert
            Assert.True(context.Accepted);
            Assert.Equal("test", context.AuthenticationScheme);
            Assert.Null(context.Description);
            Assert.Same(exception, context.Error);
            Assert.Null(context.Principal);
            Assert.Null(context.Properties);
        }
示例#5
0
        public void AuthenticateContext_Authenticated()
        {
            // Arrange
            var context = new AuthenticateContext("test");

            var principal   = new ClaimsPrincipal();
            var properties  = new Dictionary <string, string>();
            var description = new Dictionary <string, object>();

            // Act
            context.Authenticated(principal, properties, description);

            // Assert
            Assert.True(context.Accepted);
            Assert.Equal("test", context.AuthenticationScheme);
            Assert.Same(description, context.Description);
            Assert.Null(context.Error);
            Assert.Same(principal, context.Principal);
            Assert.Same(properties, context.Properties);
        }
        public Task AuthenticateAsync(AuthenticateContext context)
        {
            if (ShouldHandleScheme(context.AuthenticationScheme))
            {
                if (User != null)
                {
                    context.Authenticated(User, properties: null,
                        description: Options.AuthenticationDescriptions.FirstOrDefault(descrip =>
                            string.Equals(User.Identity.AuthenticationType, descrip.AuthenticationScheme, StringComparison.Ordinal))?.Items);
                }
                else
                {
                    context.NotAuthenticated();
                }
            }

            if (PriorHandler != null)
            {
                return PriorHandler.AuthenticateAsync(context);
            }

            return Task.FromResult(0);
        }
        public void AuthenticateContext_Failed_SetsUnusedPropertiesToDefault()
        {
            // Arrange
            var context = new AuthenticateContext("test");

            var exception = new Exception();

            context.Authenticated(new ClaimsPrincipal(), new Dictionary<string, string>(), new Dictionary<string, object>());

            // Act
            context.Failed(exception);

            // Assert
            Assert.True(context.Accepted);
            Assert.Equal("test", context.AuthenticationScheme);
            Assert.Null(context.Description);
            Assert.Same(exception, context.Error);
            Assert.Null(context.Principal);
            Assert.Null(context.Properties);
        }
示例#8
0
 public Task AuthenticateAsync(AuthenticateContext context)
 {
     context.Authenticated(new ClaimsPrincipal(), _props.Items, new Dictionary<string, object>());
     return Task.FromResult(0);
 }
示例#9
0
        private static TestServer CreateServer(CookieAuthenticationOptions options, Func<HttpContext, Task> testpath = null, Uri baseAddress = null, ClaimsTransformationOptions claimsTransform = null)
        {
            var builder = new WebHostBuilder()
                .Configure(app =>
                {
                    app.UseCookieAuthentication(options);
                    // app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme = "Cookie2" });

                    if (claimsTransform != null)
                    {
                        app.UseClaimsTransformation(claimsTransform);
                    }
                    app.Use(async (context, next) =>
                    {
                        var req = context.Request;
                        var res = context.Response;
                        PathString remainder;
                        if (req.Path == new PathString("/normal"))
                        {
                            res.StatusCode = 200;
                        }
                        else if (req.Path == new PathString("/protected"))
                        {
                            res.StatusCode = 401;
                        }
                        else if (req.Path == new PathString("/forbid")) // Simulate forbidden 
                        {
                            await context.Authentication.ForbidAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                        }
                        else if (req.Path == new PathString("/challenge"))
                        {
                            await context.Authentication.ChallengeAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                        }
                        else if (req.Path == new PathString("/signout"))
                        {
                            await context.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                        }
                        else if (req.Path == new PathString("/unauthorized"))
                        {
                            await context.Authentication.ChallengeAsync(CookieAuthenticationDefaults.AuthenticationScheme, new AuthenticationProperties(), ChallengeBehavior.Unauthorized);
                        }
                        else if (req.Path == new PathString("/protected/CustomRedirect"))
                        {
                            await context.Authentication.ChallengeAsync(new AuthenticationProperties() { RedirectUri = "/CustomRedirect" });
                        }
                        else if (req.Path == new PathString("/me"))
                        {
                            var authContext = new AuthenticateContext(CookieAuthenticationDefaults.AuthenticationScheme);
                            authContext.Authenticated(context.User, properties: null, description: null);
                            Describe(res, authContext);
                        }
                        else if (req.Path.StartsWithSegments(new PathString("/me"), out remainder))
                        {
                            var authContext = new AuthenticateContext(remainder.Value.Substring(1));
                            await context.Authentication.AuthenticateAsync(authContext);
                            Describe(res, authContext);
                        }
                        else if (req.Path == new PathString("/testpath") && testpath != null)
                        {
                            await testpath(context);
                        }
                        else
                        {
                            await next();
                        }
                    });
                })
                .ConfigureServices(services => services.AddAuthentication());
            var server = new TestServer(builder);
            server.BaseAddress = baseAddress;
            return server;
        }
示例#10
0
 private static TestServer CreateServer(Action<IServiceCollection> configureServices = null, Func<HttpContext, Task> testpath = null, Uri baseAddress = null)
 {
     var builder = new WebHostBuilder()
         .Configure(app =>
         {
             app.UseIdentity();
             app.Use(async (context, next) =>
             {
                 var req = context.Request;
                 var res = context.Response;
                 var userManager = context.RequestServices.GetRequiredService<UserManager<TestUser>>();
                 var signInManager = context.RequestServices.GetRequiredService<SignInManager<TestUser>>();
                 PathString remainder;
                 if (req.Path == new PathString("/normal"))
                 {
                     res.StatusCode = 200;
                 }
                 else if (req.Path == new PathString("/createMe"))
                 {
                     var result = await userManager.CreateAsync(new TestUser("hao"), TestPassword);
                     res.StatusCode = result.Succeeded ? 200 : 500;
                 }
                 else if (req.Path == new PathString("/createSimple"))
                 {
                     var result = await userManager.CreateAsync(new TestUser("simple"), "aaaaaa");
                     res.StatusCode = result.Succeeded ? 200 : 500;
                 }
                 else if (req.Path == new PathString("/protected"))
                 {
                     res.StatusCode = 401;
                 }
                 else if (req.Path.StartsWithSegments(new PathString("/pwdLogin"), out remainder))
                 {
                     var isPersistent = bool.Parse(remainder.Value.Substring(1));
                     var result = await signInManager.PasswordSignInAsync("hao", TestPassword, isPersistent, false);
                     res.StatusCode = result.Succeeded ? 200 : 500;
                 }
                 else if (req.Path == new PathString("/twofactorRememeber"))
                 {
                     var user = await userManager.FindByNameAsync("hao");
                     await signInManager.RememberTwoFactorClientAsync(user);
                     res.StatusCode = 200;
                 }
                 else if (req.Path == new PathString("/isTwoFactorRememebered"))
                 {
                     var user = await userManager.FindByNameAsync("hao");
                     var result = await signInManager.IsTwoFactorClientRememberedAsync(user);
                     res.StatusCode = result ? 200 : 500;
                 }
                 else if (req.Path == new PathString("/twofactorSignIn"))
                 {
                 }
                 else if (req.Path == new PathString("/me"))
                 {
                     var auth = new AuthenticateContext("Application");
                     auth.Authenticated(context.User, new AuthenticationProperties().Items, new AuthenticationDescription().Items);
                     Describe(res, auth);
                 }
                 else if (req.Path.StartsWithSegments(new PathString("/me"), out remainder))
                 {
                     var auth = new AuthenticateContext(remainder.Value.Substring(1));
                     await context.Authentication.AuthenticateAsync(auth);
                     Describe(res, auth);
                 }
                 else if (req.Path == new PathString("/testpath") && testpath != null)
                 {
                     await testpath(context);
                 }
                 else
                 {
                     await next();
                 }
             });
         })
         .ConfigureServices(services =>
         {
             services.AddIdentity<TestUser, TestRole>();
             services.AddSingleton<IUserStore<TestUser>, InMemoryStore<TestUser, TestRole>>();
             services.AddSingleton<IRoleStore<TestRole>, InMemoryStore<TestUser, TestRole>>();
             if (configureServices != null)
             {
                 configureServices(services);
             }
         });
     var server = new TestServer(builder);
     server.BaseAddress = baseAddress;
     return server;
 }