示例#1
0
        public async Task VerifyAccountControllerSignIn(bool isPersistent)
        {
            IBuilder app = new Builder.Builder(new ServiceCollection().BuildServiceProvider());

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = ClaimsIdentityOptions.DefaultAuthenticationType
            });

            var context  = new Mock <HttpContext>();
            var response = new Mock <HttpResponse>();

            context.Setup(c => c.Response).Returns(response.Object).Verifiable();
            response.Setup(r => r.SignIn(It.IsAny <ClaimsIdentity>(), It.Is <AuthenticationProperties>(v => v.IsPersistent == isPersistent))).Verifiable();
            var contextAccessor = new Mock <IContextAccessor <HttpContext> >();

            contextAccessor.Setup(a => a.Value).Returns(context.Object);
            app.UseServices(services =>
            {
                services.AddInstance(contextAccessor.Object);
                services.AddIdentity <ApplicationUser, IdentityRole>().AddInMemory().AddAuthentication();
            });

            // Act
            var user = new ApplicationUser
            {
                UserName = "******"
            };
            const string password      = "******";
            var          userManager   = app.ApplicationServices.GetService <UserManager <ApplicationUser> >();
            var          signInManager = app.ApplicationServices.GetService <SignInManager <ApplicationUser> >();

            IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password));
            var result = await signInManager.PasswordSignInAsync(user.UserName, password, isPersistent, false);

            // Assert
            Assert.Equal(SignInStatus.Success, result);
            context.VerifyAll();
            response.VerifyAll();
            contextAccessor.VerifyAll();
        }
示例#2
0
        public async Task VerifyAccountControllerSignIn(bool isPersistent)
        {
            IBuilder app = new Builder.Builder(new ServiceCollection().BuildServiceProvider());
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = ClaimsIdentityOptions.DefaultAuthenticationType
            });

            var context = new Mock<HttpContext>();
            var response = new Mock<HttpResponse>();
            context.Setup(c => c.Response).Returns(response.Object).Verifiable();
            response.Setup(r => r.SignIn(It.IsAny<ClaimsIdentity>(), It.Is<AuthenticationProperties>(v => v.IsPersistent == isPersistent))).Verifiable();
            var contextAccessor = new Mock<IContextAccessor<HttpContext>>();
            contextAccessor.Setup(a => a.Value).Returns(context.Object);
            app.UseServices(services =>
            {
                services.AddInstance(contextAccessor.Object);
                services.AddIdentity<ApplicationUser, IdentityRole>().AddInMemory().AddAuthentication();
            });

            // Act
            var user = new ApplicationUser
            {
                UserName = "******"
            };
            const string password = "******";
            var userManager = app.ApplicationServices.GetService<UserManager<ApplicationUser>>();
            var signInManager = app.ApplicationServices.GetService<SignInManager<ApplicationUser>>();

            IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password));
            var result = await signInManager.PasswordSignInAsync(user.UserName, password, isPersistent, false);

            // Assert
            Assert.Equal(SignInStatus.Success, result);
            context.VerifyAll();
            response.VerifyAll();
            contextAccessor.VerifyAll();
        }