public AppUserAuth ValidateUser(AppUser user) { AppUserAuth ret = new AppUserAuth(); AppUser authUser = null; using (var db = new PtcDbContext()) { // Attempt to validate user var result = db.Users.Where( u => u.UserName.ToLower() == user.UserName.ToLower() && u.Password == user.Password); if (result.Count() > 0) { authUser = result.First(); } } if (authUser != null) { // Build User Security Object ret = BuildUserAuthObject(authUser); } return(ret); }
private string BuildJwtToken(AppUserAuth authUser) { SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_settings.Key)); List <Claim> jwtClaims = new List <Claim> { new Claim(JwtRegisteredClaimNames.Sub, authUser.UserName), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), new Claim("isAuthenticated", authUser.IsAuthenticated.ToString().ToLower()), }; // Add custom claims from the Claim Array foreach (AppUserClaim claim in authUser.Claims) { jwtClaims.Add(new Claim(type: claim.ClaimType, value: claim.ClaimValue)); } // Create the JwtSecurityToken object JwtSecurityToken token = new JwtSecurityToken( _settings.Issuer, _settings.Audience, jwtClaims, DateTime.UtcNow, DateTime.UtcNow.AddMinutes(_settings.MinutesToExpiration), new SigningCredentials(key, SecurityAlgorithms.HmacSha256) ); // Create a string representation of the Jwt token return(new JwtSecurityTokenHandler().WriteToken(token)); }
protected string BuildJwtToken(AppUserAuth authUser) { SymmetricSecurityKey key = new 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)); } // Create the JwtSecurityToken object 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) ); // Create a string representation of the Jwt token return(new JwtSecurityTokenHandler().WriteToken(token));; }
protected AppUserAuth BuildUserAuthObject(AppUser authUser) { AppUserAuth ret = new AppUserAuth(); List <AppUserClaim> 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 claims = GetUserClaims(authUser); // Loop through all claims and // set properties of user object foreach (AppUserClaim claim in claims) { try { // TODO: Check data type of ClaimValue typeof(AppUserAuth).GetProperty(claim.ClaimType) .SetValue(ret, Convert.ToBoolean(claim.ClaimValue), null); } catch { } } // Set JWT bearer token ret.BearerToken = BuildJwtToken(ret); return(ret); }
protected string BuildJwtToken(AppUserAuth authUser) { SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_settings.Key)); //Create bare minimum JWT Claims List <Claim> jwtClaims = new List <Claim>(); jwtClaims.Add(new Claim(JwtRegisteredClaimNames.Sub, authUser.UserName)); //subject jwtClaims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())); //jwt id //Add any custom claims jwtClaims.Add(new Claim("isAuthenticated", authUser.IsAuthenticated.ToString().ToLower())); //Add custom claims from the Claim array foreach (var claims in authUser.Claims) { jwtClaims.Add(new Claim(claims.ClaimType, claims.ClaimValue)); } //Create jwt token object 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)); }
public AppUserAuth ValidateUser(AppUser user) { var ret = new AppUserAuth(); AppUser authuser = null; try { using (var db = new PtcDbContext()) { authuser = db.Users.Where( x => x.UserName.ToLower() == user.UserName.ToLower() && x.Password == user.Password ).FirstOrDefault(); } if (authuser != null) { ret = BuildUserAuthObject(authuser); } } catch (Exception ex) { throw new Exception("Exception while retreving a claim", ex); } return(ret); }
protected string BuildJwtToken(AppUserAuth authUser) { SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_settings.Key)); 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 JwtClaims.Add(new Claim("isAuthenticated", authUser.IsAuthenticated.ToString().ToLower())); authUser.Claims.ForEach(i => { JwtClaims.Add(new Claim(i.ClaimType, i.ClaimValue.ToString().ToLower())); }); // create the jwt token here. JwtSecurityToken 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) { SymmetricSecurityKey IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Settings.Key)); List <Claim> jwtClaims = new List <Claim>(); jwtClaims.Add(new Claim(JwtRegisteredClaimNames.Sub, authUser.UserName)); jwtClaims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())); foreach (var claim in authUser.Claims) { jwtClaims.Add(new Claim(claim.ClaimType, claim.ClaimValue)); } jwtClaims.Add(new Claim("IsAuthenticated", authUser.IsAuthenticated.ToString().ToLower())); var token = new JwtSecurityToken( issuer: Settings.Issuer, audience: Settings.Audience, claims: jwtClaims, notBefore: DateTime.UtcNow, expires: DateTime.UtcNow.AddMinutes(Settings.MinutesToExpire), signingCredentials: new SigningCredentials(IssuerSigningKey, SecurityAlgorithms.HmacSha256) ); return(new JwtSecurityTokenHandler().WriteToken(token)); }
protected AppUserAuth BuildUserAuthObject(AppUser authUser) { AppUserAuth userAuth = new AppUserAuth(); List <AppUserClaim> claims = new List <AppUserClaim>(); userAuth.UserName = authUser.UserName; userAuth.IsAuthenticated = true; userAuth.BearerToken = new Guid().ToString(); userAuth.Claims = GetUserClaims(authUser); userAuth.BearerToken = BuildJwtToken(userAuth); return(userAuth); }
protected string BuildJwtToken(AppUserAuth authUser) { SymmetricSecurityKey key = new 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 jwtClaims.Add(new Claim("isAuthenticated", authUser.IsAuthenticated.ToString().ToLower())); #region commented old specific claims //jwtClaims.Add(new Claim("canAccessProducts", // authUser.CanAccessProducts.ToString().ToLower())); //jwtClaims.Add(new Claim("canAddProduct", // authUser.CanAddProduct.ToString().ToLower())); //jwtClaims.Add(new Claim("canSaveProduct", // authUser.CanSaveProduct.ToString().ToLower())); //jwtClaims.Add(new Claim("canAccessCategories", // authUser.CanAccessCategories.ToString().ToLower())); //jwtClaims.Add(new Claim("canAddCategory", // authUser.CanAddCategory.ToString().ToLower())); #endregion //add custom claims from the claim array foreach (var claim in authUser.Claims) { jwtClaims.Add(new Claim(claim.ClaimType, claim.ClaimValue)); } // Create the JwtSecurityToken object 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) ); // Create a string representation of the Jwt token return(new JwtSecurityTokenHandler().WriteToken(token));; }
protected AppUserAuth BuildUserAuthObject(AppUser user) { AppUserAuth ret = new AppUserAuth(); List <AppUserClaim> claims = new List <AppUserClaim>(); ret.UserName = user.UserName; ret.IsAuthenticated = true; ret.BearerToken = new Guid().ToString(); ret.Claims = GetUserClaimes(user); ret.BearerToken = BuildJwtToken(ret); return(ret); }
protected AppUserAuth BuildUserAuthObject(AppUser authUser) { AppUserAuth ret = new AppUserAuth(); List <AppUserClaim> claims = new List <AppUserClaim>(); //set user properties ret.UserName = authUser.UserName; ret.IsAuthenticated = true; ret.BearerToken = new Guid().ToString(); //get all claims from this user ret.Claims = GetUserClaims(authUser); ret.BearerToken = BuildJwtToken(ret); return(ret); }
private AppUserAuth BuildUserAuthObject(AppUser authUser) { AppUserAuth ret = new AppUserAuth { // Set User Properties UserName = authUser.UserName, IsAuthenticated = true, BearerToken = new Guid().ToString() }; // Get all claims for the user ret.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.AppUser.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) { AppUserAuth ret = new AppUserAuth(); AppUser authUser = null; using (PtcDbContext db = new PtcDbContext()) { // Attempt to validate user authUser = db.Users.FirstOrDefault(u => string.Equals(u.UserName, user.UserName, StringComparison.CurrentCultureIgnoreCase) && u.Password == user.Password); } if (authUser != null) { ret = BuildUserAuthObject(authUser); } return(ret); }
public AppUserAuth AuthenticateUser(AppUser user) { AppUserAuth ret = new AppUserAuth(); AppUser authUser = null; using (var db = new PtcDbContext()) { // Attempt to validate user authUser = db.Users.Where(u => u.UserName.ToLower() == user.UserName.ToLower() && u.Password == user.Password).FirstOrDefault(); } if (authUser != null) { // Build User Security Object ret = BuildUserAuthObject(authUser); } return(ret); }
public AppUserAuth GetNewUserClaims(AppUser user) { AppUserAuth ret = new AppUserAuth(); AppUser authUser = null; try { using (var db = new PtcDbContext()) { if (user != null) { db.Users.Add(user); db.SaveChanges(); authUser = db.Users.Where( u => u.UserName.ToLower() == user.UserName.ToLower() && u.Password == user.Password).FirstOrDefault(); AppUserClaim userClaim = new AppUserClaim(); userClaim.UserId = authUser.UserId; userClaim.ClaimType = "CanAccessMenu"; userClaim.ClaimValue = "true"; db.Claims.Add(userClaim); db.SaveChanges(); if (authUser != null) { //build usersecurity object ret = BuildUserAuthObject(authUser); } } } } catch (Exception ex) { throw new Exception( "Exception trying to create new user.", ex); } return(ret); }