public async Task <IActionResult> Login(LoginViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var loginDto = _mapper.Map <LoginDto>(model); var serviceResult = await _userService.LoginAsync(loginDto); if (!serviceResult.Success) { foreach (var error in serviceResult.ErrorMessages) { ModelState.AddModelError(error.Key, error.Value); } return(View(model)); } await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(CookieAuthenticationExtensions.SignIn(serviceResult.Result))); return(Redirect("/")); }
public void ConfigureAuth(IAppBuilder app) { AppBuilderExtensions.CreatePerOwinContext <ApplicationDbContext>(app, (Func <M0>) new Func <ApplicationDbContext>(ApplicationDbContext.Create)); AppBuilderExtensions.CreatePerOwinContext <ApplicationUserManager>(app, (Func <IdentityFactoryOptions <M0>, IOwinContext, M0>) new Func <IdentityFactoryOptions <ApplicationUserManager>, IOwinContext, ApplicationUserManager>(ApplicationUserManager.Create)); AppBuilderExtensions.CreatePerOwinContext <ApplicationSignInManager>(app, (Func <IdentityFactoryOptions <M0>, IOwinContext, M0>) new Func <IdentityFactoryOptions <ApplicationSignInManager>, IOwinContext, ApplicationSignInManager>(ApplicationSignInManager.Create)); IAppBuilder iappBuilder = app; CookieAuthenticationOptions authenticationOptions1 = new CookieAuthenticationOptions(); ((AuthenticationOptions)authenticationOptions1).set_AuthenticationType("ApplicationCookie"); authenticationOptions1.set_LoginPath(new PathString("/Account/Login")); CookieAuthenticationOptions authenticationOptions2 = authenticationOptions1; CookieAuthenticationProvider authenticationProvider1 = new CookieAuthenticationProvider(); authenticationProvider1.set_OnValidateIdentity(SecurityStampValidator.OnValidateIdentity <ApplicationUserManager, ApplicationUser>(TimeSpan.FromMinutes(30.0), (Func <M0, M1, Task <ClaimsIdentity> >)((manager, user) => user.GenerateUserIdentityAsync((UserManager <ApplicationUser>)manager)))); CookieAuthenticationProvider authenticationProvider2 = authenticationProvider1; authenticationOptions2.set_Provider((ICookieAuthenticationProvider)authenticationProvider2); CookieAuthenticationOptions authenticationOptions3 = authenticationOptions1; CookieAuthenticationExtensions.UseCookieAuthentication(iappBuilder, authenticationOptions3); AppBuilderExtensions.UseExternalSignInCookie(app, "ExternalCookie"); AppBuilderExtensions.UseTwoFactorSignInCookie(app, "TwoFactorCookie", TimeSpan.FromMinutes(5.0)); AppBuilderExtensions.UseTwoFactorRememberBrowserCookie(app, "TwoFactorRememberBrowser"); }