protected string BuildJwtToken(AppUserAuth authUser) { var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_settings.Key)); var jwtClaims = new List <Claim>(); jwtClaims.Add(new Claim(JwtRegisteredClaimNames.Sub, authUser.UserName)); jwtClaims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())); jwtClaims.Add(new Claim("isAuthenticated", authUser.IsAuthenticated.ToString().ToLower())); //add custom claims from the claim array foreach (var claim in authUser.Claims) { jwtClaims.Add(new Claim(claim.ClaimType, claim.ClaimValue)); } var token = new JwtSecurityToken( issuer: _settings.Issuer, audience: _settings.Audience, claims: jwtClaims, notBefore: DateTime.UtcNow, expires: DateTime.UtcNow.AddMinutes( _settings.MinutesToExpiration ), signingCredentials: new SigningCredentials( key, SecurityAlgorithms.HmacSha256 ) ); return(new JwtSecurityTokenHandler().WriteToken(token)); }
protected string BuildJwtToken(AppUserAuth authUser) { Microsoft.IdentityModel.Tokens.SymmetricSecurityKey key = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(_settings.Key)); //Create standard JWT claims List <Claim> jwtClaims = new List <Claim> (); jwtClaims.Add(new Claim(JwtRegisteredClaimNames.Sub, authUser.UserName)); jwtClaims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())); //Add custom claims foreach (var claim in authUser.Claims) { jwtClaims.Add(new Claim(claim.ClaimType, claim.ClaimValue)); } var token = new JwtSecurityToken( issuer: _settings.Issuer, audience: _settings.Audience, claims: jwtClaims, notBefore: DateTime.UtcNow, expires: DateTime.UtcNow.AddMinutes(_settings.MinutesToExpiration), signingCredentials: new Microsoft.IdentityModel.Tokens.SigningCredentials(key, Microsoft.IdentityModel.Tokens.SecurityAlgorithms.HmacSha256) ); return(new JwtSecurityTokenHandler().WriteToken(token)); }
protected string BuildJwtToken(AppUserAuth userAuth) { SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.Key)); // Create standard JWT claims var jwtClaims = new List <Claim> { new Claim(JwtRegisteredClaimNames.Sub, userAuth.UserName), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()) }; // Add custom claims jwtClaims.Add(new Claim("isAuthenticated", userAuth.IsAuthenticated.ToString().ToLower())); // Add custom claims from the Claim array foreach (var claim in userAuth.Claims) { jwtClaims.Add(new Claim(claim.ClaimType, claim.ClaimValue)); } // Create the JwtSecurityToken object var token = new JwtSecurityToken( issuer: _jwtSettings.Issuer, audience: _jwtSettings.Audience, claims: jwtClaims, notBefore: DateTime.UtcNow, expires: DateTime.UtcNow.AddMinutes(_jwtSettings.MinutesToExpiration), signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256) ); // Create a string representation of the JWT token return(new JwtSecurityTokenHandler().WriteToken(token)); }
public AppUserAuth BuildUserAuthObject(AppUser authUser) { AppUserAuth ret = new AppUserAuth(); ret.UserName = authUser.UserName; ret.IsAuthenticated = true; ret.BearerToken = new Guid().ToString(); ret.Claims = GetUserClaims(authUser); ret.BearerToken = BuildJwtToken(ret); return(ret); }
protected AppUserAuth BuildUserAuthObject(AppUser authUser) { var ret = new AppUserAuth { UserName = authUser.UserName, IsAuthenticated = true, BearerToken = new Guid().ToString(), Claims = GetUserClaims(authUser) }; ret.BearerToken = BuildJwtToken(ret); return(ret); }
public AppUserAuth ValidateUser(AppUser user) { AppUserAuth ret = new AppUserAuth(); AppUser authUser = null; using (var db = new PtcDbContext()) { authUser = db.Users.Where(u => u.UserName.ToLower() == user.UserName.ToLower() && u.Password == user.Password).FirstOrDefault(); } if (authUser != null) { ret = BuildUserAuthObject(authUser); } return(ret); }
public AppUserAuth ValidateUser(AppUser user) { var ret = new AppUserAuth(); AppUser authUser = null; using (var db = new PtcDbContext()) { // Attempt to validate user authUser = db.Users.FirstOrDefault(u => u.Password == user.Password); } if (authUser != null) { // Build user security object ret = BuildUserAuthObject(authUser); } return(ret); }
protected AppUserAuth BuildUserAuthObject(AppUser authUser) { var ret = new AppUserAuth(); var claims = new List <AppUserClaim>(); // Set User Properties ret.UserName = authUser.UserName; ret.IsAuthenticated = true; ret.BearerToken = new Guid().ToString(); // Get all claims for this user ret.Claims = GetUserClaims(authUser); // ret.Claims = GetUserClaims(authUser); // Set JWT bearer token ret.BearerToken = BuildJwtToken(ret); return(ret); }