public async Task <IActionResult> Register(UserEntity user) { if (ModelState.IsValid) { ModelStateEntry modelStateEntry = default(ModelStateEntry); ModelState.TryGetValue(user.Account, out modelStateEntry); var file = Request.Form.Files["Photo"]; if (file != null) { string newfileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); string temporary = Path.Combine(_hosting.WebRootPath, "Resource/Photo");//临时保存分块的目录 if (!Directory.Exists(temporary)) { Directory.CreateDirectory(temporary); } string filePath = Path.Combine(temporary, newfileName); using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) { await file.CopyToAsync(fs); } user.Photo = "src/Photo/" + newfileName; } user.PassWord = MD5Encrypt.MD5Encrypt16(user.PassWord); bool isOK = await _userService.Register(user) > 0; return(RedirectToAction(nameof(Login))); } else { var depts = await _deptService.GetList(Util.Extension.ExpressionExtension.True <DepartmentEntity>()); ViewBag.Depts = depts; user.CreateDate = DateTime.Now; return(View(user)); } }
public async Task <IActionResult> Login(IFormCollection forms) { try { string account = forms["Account"], password = forms["PassWord"], isRemember = forms["IsRememberMe"]; UserEntity user = await _userService.GetEntity(p => p.Account == account); if (user is null) { throw new Exception("账号或密码错误"); } if (!(user.PassWord.Equals(password) || user.PassWord.Equals(MD5Encrypt.MD5Encrypt16(password)))) { throw new Exception("账号或密码错误"); } //记住密码 var claims = new List <Claim> { new Claim(ClaimTypes.Name, user.UserName), new Claim(ClaimTypes.WindowsAccountName, user.Account), new Claim(ClaimTypes.Role, user.DeptInfo.DeptName), new Claim(ClaimTypes.PrimarySid, user.ID.ToString()), new Claim(ClaimTypes.GroupSid, user.DeptId.ToString()), new Claim(ClaimTypes.WindowsDeviceGroup, user.DeptInfo.DeptName) }; var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); var authProperties = new AuthenticationProperties { // Whether the authentication session is persisted across // multiple requests. When used with cookies, controls // whether the cookie's lifetime is absolute (matching the // lifetime of the authentication ticket) or session-based. IsPersistent = true, // The time at which the authentication ticket expires. A // value set here overrides the ExpireTimeSpan option of // CookieAuthenticationOptions set with AddCookie. ExpiresUtc = DateTime.UtcNow.AddMinutes(30), // Refreshing the authentication session should be allowed. AllowRefresh = true, // The time at which the authentication ticket was issued. //IssuedUtc = <DateTimeOffset>, RedirectUri = Url.Action(nameof(Login)) // The full path or absolute URI to be used as an http // redirect response value. }; await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); if (!isRemember.IsEmpty() && isRemember.Equals("remember-me")) { //采用对称加密,对用户信息进行加密.。写入cookie string cookieValue = JsonConvert.SerializeObject(new { user.Account, user.PassWord }); string encyptString = _protector.Protect(cookieValue);//加密 HttpContext.Response.Cookies.Append("User_Account", encyptString, new CookieOptions() { Expires = DateTimeOffset.UtcNow.AddDays(30), }); } return(RedirectToAction("Index", "Home")); } catch (Exception ex) { ModelState.AddModelError("Error", ex.Message); } return(View()); }