public BasicAuthenticationOptions(string realm, BasicAuthenticationMiddleware.CredentialValidationFunction validationFunction) : base("Basic") { Realm = realm; CredentialValidationFunction = validationFunction; }
public BasicAuthenticationOptions(string realm, BasicAuthenticationMiddleware.CredentialValidationFunction credentialValidationFunction) : base("Basic") { this.Realm = realm; this.CredentialValidationFunction = credentialValidationFunction; this.AuthenticationMode = AuthenticationMode.Active; }
public async Task TestAuthOptions(string header, string user, string password, bool asName, bool asId, bool asEmail) { var currentUser = new IdentityUser { UserName = user }; SetupMockUserManager(user, asName, asId, asEmail, currentUser); SetupMockSignInManagerSuccess(password, currentUser); var sp = new ServiceCollection() .Configure <BasicAuthenticationOptions>(options => { options.FindsByName = asName; options.FindsById = asId; options.FindsByEmail = asEmail; }) .AddTransient <UserManager <IdentityUser> >(_ => MockUserManager.Object) .AddTransient <SignInManager <IdentityUser> >(_ => MockSignInManager.Object) .AddTransient(_ => MockLogger.Object) .BuildServiceProvider(); var contextMock = BuildContextMock(header, sp); var next = new RequestDelegate(_ => Task.CompletedTask); var middleware = new BasicAuthenticationMiddleware <IdentityUser>(next); await middleware.InvokeAsync(contextMock.Object); Assert.True(contextMock.Object.User.Identity.IsAuthenticated); }
public async Task MiddlewareExtensionsBasicAuthenticationMiddlewareNotSignedIn() { // Arrange var httpContext = new DefaultHttpContext(); var authMiddleware = new BasicAuthenticationMiddleware(next: (innerHttpContext) => Task.FromResult(0)); // Act await authMiddleware.Invoke(httpContext); Assert.IsNotNull(httpContext); }
public async void TestEmptyContext() { var context = new DefaultHttpContext(); var next = new RequestDelegate(_ => Task.CompletedTask); var oldUser = context.User; var middleware = new BasicAuthenticationMiddleware <IdentityUser>(next); await middleware.InvokeAsync(context); // User が変更されないこと Assert.Equal(oldUser, context.User); // 認証されていないこと Assert.False(context.User.Identity.IsAuthenticated); }
public async Task GivenNoAuthentication_ShouldReturn401() { var middleware = new BasicAuthenticationMiddleware( ctx => Task.CompletedTask, new BasicAuthenticationConfiguration("test", "token")); var httpContext = new DefaultHttpContext(); httpContext.Request.Path = "/api/whatever"; await middleware.Invoke(httpContext); httpContext.Response.StatusCode .Should() .Be(401); }
public async Task GivenInValidAuthentication_ShouldReturn401() { var middleware = new BasicAuthenticationMiddleware( ctx => Task.CompletedTask, new BasicAuthenticationConfiguration("test", "token")); var httpContext = new DefaultHttpContext(); httpContext.Request.Path = "/api/whatever"; // 'dGVzdDp0b2tlbg==' is 'test:token' base64 encoded httpContext.Request.Headers.Add("Authorization", "Basic GVzdDp0b2tlbg=="); await middleware.Invoke(httpContext); httpContext.Response.StatusCode .Should() .Be(401); }
public async Task BasicAuthenticationMiddlewareLoginTest() { // Arrange var iUserManager = _serviceProvider.GetRequiredService <IUserManager>(); var httpContext = _serviceProvider.GetRequiredService <IHttpContextAccessor>().HttpContext; var userId = "TestUserA"; var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, userId) }; httpContext.User = new ClaimsPrincipal(new ClaimsIdentity(claims)); httpContext.RequestServices = _serviceProvider; var schemeProvider = _serviceProvider.GetRequiredService <IAuthenticationSchemeProvider>(); var controller = new AccountController(_userManager, new AppSettings(), new FakeAntiforgery(), new FakeSelectorStorage()) { ControllerContext = { HttpContext = httpContext } }; // Make new account; var newAccount = new RegisterViewModel { Password = "******", ConfirmPassword = "******", Email = "test" }; // Arange > new account await iUserManager.SignUpAsync("test", "email", "test", "test"); // base64 dGVzdDp0ZXN0 > test:test httpContext.Request.Headers["Authorization"] = "Basic dGVzdDp0ZXN0"; // Call the middleware app var basicAuthMiddleware = new BasicAuthenticationMiddleware(_onNext); await basicAuthMiddleware.Invoke(httpContext); Assert.AreEqual(true, httpContext.User.Identity.IsAuthenticated); }
public async Task GivenNoAuthentication_ShouldNotCallNext() { var wasCalled = false; var middleware = new BasicAuthenticationMiddleware( ctx => { wasCalled = true; return(Task.CompletedTask); }, new BasicAuthenticationConfiguration("test", "token")); var httpContext = new DefaultHttpContext(); httpContext.Request.Path = "/api/whatever"; await middleware.Invoke(httpContext); wasCalled .Should() .BeFalse(); }
public async Task GivenInValidAuthentication_ShouldNotCallNext() { var wasCalled = false; var middleware = new BasicAuthenticationMiddleware( ctx => { wasCalled = true; return(Task.CompletedTask); }, new BasicAuthenticationConfiguration("test", "token")); var httpContext = new DefaultHttpContext(); httpContext.Request.Path = "/api/whatever"; // 'dGVzdDp0b2tlbg==' is 'test:token' base64 encoded httpContext.Request.Headers.Add("Authorization", "Basic GVzdDp0b2tlbg=="); await middleware.Invoke(httpContext); wasCalled .Should() .BeFalse(); }
public static void UseBasicAuthentication(this IAppBuilder app, string realm, BasicAuthenticationMiddleware.CredentialValidationFunction validationFunction) { var options = new BasicAuthenticationOptions(realm, validationFunction); app.Use<BasicAuthenticationMiddleware>(options); }
public static IAppBuilder UseBasicAuthentication(this IAppBuilder appBuilder, string realm, BasicAuthenticationMiddleware.CredentialVlidationFunction validationFunction) { var options = new BasicAuthenticationOptions(realm, validationFunction); return appBuilder.UseBasicAuthentication(options); }