public Task AuthenticateAsync(AuthenticateContext context) { if (User == null) { context.NotAuthenticated(); } else if (Scheme == null || Scheme == context.AuthenticationScheme) { context.Authenticated(User, new Dictionary<string, string>(), new Dictionary<string, object>()); } return Task.FromResult(0); }
public async Task AuthenticateAsync(AuthenticateContext context) { if (PriorHandler != null) { await PriorHandler.AuthenticateAsync(context); if (_transform != null && context?.Principal != null) { context.Authenticated( await _transform.TransformAsync(context.Principal), context.Properties, context.Description); } } }
private void ApplyTransform(AuthenticateContext context) { if (_transform != null) { // REVIEW: this cast seems really bad (missing interface way to get the result back out?) var authContext = context as AuthenticateContext; if (authContext?.Principal != null) { context.Authenticated( _transform.Invoke(authContext.Principal), authContext.Properties, authContext.Description); } } }
public Task AuthenticateAsync(AuthenticateContext context) { if (ShouldHandleScheme(context.AuthenticationScheme)) { if (User != null) { context.Authenticated(User, properties: null, description: Options.AuthenticationDescriptions.Where(descrip => string.Equals(User.Identity.AuthenticationType, descrip.AuthenticationScheme, StringComparison.Ordinal)).FirstOrDefault()?.Items); } else { context.NotAuthenticated(); } } if (PriorHandler != null) { return PriorHandler.AuthenticateAsync(context); } return Task.FromResult(0); }
private static TestServer CreateServer(Action<IServiceCollection> configureServices = null, Func<HttpContext, Task> testpath = null, Uri baseAddress = null) { var server = TestServer.Create(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(); } }); }, services => { services.AddIdentity<TestUser, TestRole>(); services.AddSingleton<IUserStore<TestUser>, InMemoryUserStore<TestUser>>(); services.AddSingleton<IRoleStore<TestRole>, InMemoryRoleStore<TestRole>>(); if (configureServices != null) { configureServices(services); } }); server.BaseAddress = baseAddress; return server; }
public virtual async Task AuthenticateAsync(AuthenticateContext context) { if (ShouldHandleScheme(context.AuthenticationScheme)) { var ticket = await AuthenticateAsync(); if (ticket?.Principal != null) { AuthenticateCalled = true; context.Authenticated(ticket.Principal, ticket.Properties.Items, BaseOptions.Description.Items); } else { context.NotAuthenticated(); } } if (PriorHandler != null) { await PriorHandler.AuthenticateAsync(context); } }
private static TestServer CreateServer(Action<CookieAuthenticationOptions> configureOptions, Func<HttpContext, Task> testpath = null, Uri baseAddress = null, Action<ClaimsTransformationOptions> claimsTransform = null) { var server = TestServer.Create(app => { app.UseCookieAuthentication(configureOptions); if (claimsTransform != null) { app.UseClaimsTransformation(); } 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("/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(); } }); }, services => { services.AddAuthentication(); if (claimsTransform != null) { services.ConfigureClaimsTransformation(claimsTransform); } }); server.BaseAddress = baseAddress; return server; }
public async Task AuthenticateAsync(AuthenticateContext context) { if (ShouldHandleScheme(context.AuthenticationScheme)) { // Calling Authenticate more than once should always return the original value. var ticket = await HandleAuthenticateOnceAsync(); if (ticket?.Principal != null) { context.Authenticated(ticket.Principal, ticket.Properties.Items, BaseOptions.Description.Items); } else { context.NotAuthenticated(); } } if (PriorHandler != null) { await PriorHandler.AuthenticateAsync(context); } }