public string Signin(AuthViewModel account) { var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes("MY_BIG_SECRET_KEY_ASDWQEWEWWEQ@#@!#!@#QWE!@!#!@#!@#!@EWQE!@#!@#!@#QWE!@#!@#@!LKSHDJFLSDKFW@#($)(#)32234"); //var permissions = JsonConvert.SerializeObject(new List<int>(){1 , 3}); var permissions = JsonConvert.SerializeObject(account.Permissions); var claims = new List <Claim> { new Claim("Fullname", account.Fullname), new Claim("Mobile", account.Mobile), new Claim("Username", account.Username), new Claim("AccountId", account.Id.ToString()), new Claim("Permition", permissions), new Claim(ClaimTypes.Role, account.RoleId.ToString()), }; var tokenDescription = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), Expires = DateTime.UtcNow.AddSeconds(10), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescription); var tokenString = tokenHandler.WriteToken(token); return(tokenString); }
public void SignIn(AuthViewModel account) { var permission = JsonConvert.SerializeObject(account.Permissions); var claims = new List <Claim> { new Claim("AccountId", account.Id.ToString()), new Claim(ClaimTypes.Name, account.FullName), new Claim(ClaimTypes.Role, account.RoleId.ToString()), new Claim("Username", account.Username), // Or Use ClaimTypes.NameIdentifier new Claim("Fullname", account.FullName), new Claim("Picture", account.Picture), new Claim("permissions", permission), }; var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); var authProperties = new AuthenticationProperties { ExpiresUtc = DateTimeOffset.UtcNow.AddDays(1) }; _contextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); }
public AuthViewModel GetCurrentUserInfo() { var authViewModel = new AuthViewModel(); if (IsAuthenticated()) { var claims = _contextAccessor.HttpContext.User.Claims.ToList(); authViewModel.FullName = claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value; authViewModel.Id = long.Parse(claims.FirstOrDefault(x => x.Type == "AccountId").Value); authViewModel.RoleId = long.Parse(claims.FirstOrDefault(x => x.Type == ClaimTypes.Role).Value); authViewModel.Username = claims.FirstOrDefault(x => x.Type == "Username").Value; authViewModel.Role = Roles.GetRoleBy(authViewModel.RoleId); } return(authViewModel); }
public AuthViewModel CurrentAccountInfo() { var result = new AuthViewModel(); if (!IsAuthenticated()) { return(result); } var claims = _contextAccessor.HttpContext.User.Claims.ToList(); result.Id = long.Parse(claims.FirstOrDefault(x => x.Type == "AccountId").Value); result.Username = claims.FirstOrDefault(x => x.Type == "Username").Value; result.RoleId = long.Parse(claims.FirstOrDefault(x => x.Type == ClaimTypes.Role).Value); result.Fullname = claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value; result.Role = Roles.GetRoleBy(result.RoleId); return(result); }
public void SingIn(AuthViewModel account) { var claims = new List <Claim> { new Claim("AccountId", account.AccountId.ToString()), new Claim(ClaimTypes.Name, account.FullName), new Claim(ClaimTypes.Role, account.RoleId.ToString()), new Claim("UserName", account.UserName), // Or Use ClaimTypes.NameIdentifier }; var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); var authProperties = new AuthenticationProperties { ExpiresUtc = DateTimeOffset.UtcNow.AddDays(4) }; _contextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); }