public static string GenerateTokenJwt(Conductor conductor, string secretKey, string audienceToken, string issuerToken, string expireTime, Mapeo _context) { //TODO: appsetting for Demo JWT - protect correctly this settings var securityKey = new SymmetricSecurityKey(System.Text.Encoding.Default.GetBytes(secretKey)); var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature); // create a claimsIdentity ClaimsIdentity claimsIdentity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, conductor.Usuario), new Claim(ClaimTypes.Role, conductor.Rol.ToString()) }); // create token to the user var tokenHandler = new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler(); //Se crea objeto que tiene el estandar JWT var jwtSecurityToken = tokenHandler.CreateJwtSecurityToken( audience: audienceToken, issuer: issuerToken, subject: claimsIdentity, notBefore: DateTime.Now, //fecha de creación del token expires: DateTime.Now.AddMinutes(Convert.ToInt32(expireTime)), //fecha de expiración de token signingCredentials: signingCredentials); var jwtTokenString = tokenHandler.WriteToken(jwtSecurityToken); ConductorTokenLogin token = new ConductorTokenLogin(); token.FechaGenerado = DateTime.Now; token.FechaVigencia = DateTime.Now.AddMinutes(Convert.ToInt32(expireTime)); token.IdConductor = conductor.IdConductor; token.Token = jwtTokenString; new LConductor(_context).guardarToken(token); return(jwtTokenString); }
//Guarda token de login (S) public async Task almacenarTokenLogin(ConductorTokenLogin token) { using (var db = new MapeoConductor()) { db.tokenLogin.Add(token); await db.SaveChangesAsync(); } }
//Guarda token de login (S) public async Task almacenarTokenLogin(ConductorTokenLogin token) { using (var db = _context) { db.tokenLoginCo.Add(token); await db.SaveChangesAsync(); } }
//Elimina token al eliminar cuenta y cerrar sesion (S) public async Task eliminarToken(ConductorTokenLogin token) { using (var db = new MapeoConductor()) { ConductorTokenLogin tokenC = db.tokenLogin.Where(x => x.IdConductor == token.IdConductor).FirstOrDefault(); db.tokenLogin.Remove(tokenC); await db.SaveChangesAsync(); } }
//Elimina token al eliminar cuenta y cerrar sesion (S) public async Task eliminarToken(ConductorTokenLogin token) { using (var db = _context) { ConductorTokenLogin tokenC = db.tokenLoginCo.Where(x => x.IdConductor == token.IdConductor).First(); db.tokenLoginCo.Remove(tokenC); await db.SaveChangesAsync(); } }
public async Task <ActionResult> delEliminarToken(string usuario) { Conductor conductor1 = new LConductor(_context).mostrarDatosLogin(usuario); int idConductor = conductor1.IdConductor; ConductorTokenLogin token = new ConductorTokenLogin(); token.IdConductor = idConductor; await new LEliminarConductor(_context).eliminarToken(token); return(Ok("El token fue eliminado")); }
public async Task <IHttpActionResult> delEliminarToken(string usuario) { try { Conductor conductor1 = new LConductor().mostrarDatosLogin(usuario); int idConductor = conductor1.IdConductor; ConductorTokenLogin token = new ConductorTokenLogin(); token.IdConductor = idConductor; await new LEliminarConductor().eliminarToken(token); return(Ok("El token fue eliminado")); }catch (Exception ex) { return(BadRequest("el usuario no existe")); } }
public static string GenerateTokenJwt(Conductor conductor) { //TODO: appsetting for Demo JWT - protect correctly this settings //Llaves para permitir manipulación o cambios var secretKey = ConfigurationManager.AppSettings["JWT_SECRET_KEY"]; var audienceToken = ConfigurationManager.AppSettings["JWT_AUDIENCE_TOKEN"]; var issuerToken = ConfigurationManager.AppSettings["JWT_ISSUER_TOKEN"]; var expireTime = ConfigurationManager.AppSettings["JWT_EXPIRE_MINUTES"]; var securityKey = new SymmetricSecurityKey(System.Text.Encoding.Default.GetBytes(secretKey)); var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature); // create a claimsIdentity ClaimsIdentity claimsIdentity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, conductor.Usuario), new Claim(ClaimTypes.Role, conductor.Rol.ToString()) }); // create token to the user var tokenHandler = new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler(); //Se crea objeto que tiene el estandar JWT var jwtSecurityToken = tokenHandler.CreateJwtSecurityToken( audience: audienceToken, issuer: issuerToken, subject: claimsIdentity, notBefore: DateTime.Now, //fecha de creación del token expires: DateTime.Now.AddMinutes(Convert.ToInt32(expireTime)), //fecha de expiración de token signingCredentials: signingCredentials); var jwtTokenString = tokenHandler.WriteToken(jwtSecurityToken); ConductorTokenLogin token = new ConductorTokenLogin(); token.FechaGenerado = DateTime.Now; token.FechaVigencia = DateTime.Now.AddMinutes(Convert.ToInt32(expireTime)); token.IdConductor = conductor.IdConductor; token.Token = jwtTokenString; new LConductor().guardarToken(token); return(jwtTokenString); }
public async Task eliminarToken(ConductorTokenLogin token) //S { await new DaoConductor(_context).eliminarToken(token); }
public async Task guardarToken(ConductorTokenLogin token) //S { await new DaoSeguridadConductor(_context).almacenarTokenLogin(token); }