public async Task <IActionResult> Login([FromBody] BaseAuthModel loginModel, CancellationToken cancellationToken) { var remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress; string result = ""; if (remoteIpAddress != null) { if (remoteIpAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) { remoteIpAddress = System.Net.Dns.GetHostEntry(remoteIpAddress).AddressList .First(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork); } result = remoteIpAddress.ToString(); } var model = new LoginModel { IpAddress = result, Login = loginModel.Login, Password = loginModel.Password }; var status = await _authService.Login(model, cancellationToken); if (!status.Success) { return(BadRequest(status)); } return(Ok(status)); }
private async Task Authentificate(BaseAuthModel authModel, HttpContext context) { // создаем один claim var claims = new List <Claim> { new Claim(ClaimsIdentity.DefaultNameClaimType, authModel.Login) }; // создаем объект ClaimsIdentity ClaimsIdentity id = new ClaimsIdentity(claims, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType); // установка аутентификационных куки await context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(id)); }
public async Task <User> TryLogin(BaseAuthModel authModel, HttpContext context) { var user = await _userRepository.GetByCredentials(authModel.Login, authModel.Password); if (user is not null) { await Task.Delay(5000); await Authentificate(authModel, context); return(user); } return(null); }
public async Task <ActionResult> Login(string login, string password, bool createPersistentCookie) { //var userName = "******"; //var password = "******"; //var createPersistentCookie = true; var token = ""; BaseAuthModel restResult = BaseRestClient <GetUserInformationModel> .Authorizatize("http://localhost:5117/auth", login, password); if (restResult != null) { token = restResult.Token; if (!token.IsNullOrWhiteSpace()) { FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, //version login, // user name DateTime.Now, //creation DateTime.Now.AddMinutes(30), //Expiration (you can set it to 1 month true, //Persistent null); // additional informations var encryptedCookie = FormsAuthentication.Encrypt(authTicket); var authCookie = new HttpCookie("AdventureWorksUser", encryptedCookie); if (createPersistentCookie) { authCookie.Expires = authTicket.Expiration; } authCookie.HttpOnly = true; authCookie.Path = FormsAuthentication.FormsCookiePath; authCookie["UserName"] = login; authCookie["Token"] = token; authCookie["Claims"] = string.Join(",", restResult.Claims.ToArray()); HttpContext.Response.Cookies.Remove("AdventureWorksUser"); HttpContext.Response.SetCookie(authCookie); FormsAuthentication.SetAuthCookie(login, createPersistentCookie); return(RedirectToAction("Index", "Home")); } } return(View()); }
public async Task <User> Register(BaseAuthModel authModel, HttpContext context) { var existingUser = await _userRepository.GetByCredentials(authModel.Login, authModel.Password); if (existingUser is null) { var newUser = new User { Id = Guid.NewGuid(), Login = authModel.Login, Password = authModel.Password, CompanyId = CompanyHelper.DefaultCompany.Id }; await _userRepository.Create(newUser); await Authentificate(authModel, context); return(newUser); } return(null); }