public static Token CreateToken(User u, string deviceName) { string key = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1"; var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key)); var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature); var header = new JwtHeader(credentials); var payload = new JwtPayload { { "Name", u.Name }, { "Surname", u.Surname }, { "Email", u.Email }, { "DeviceName", deviceName }, { "CreationDateTime", System.DateTime.Now } }; var secToken = new JwtSecurityToken(header, payload); var handler = new JwtSecurityTokenHandler(); var tokenString = handler.WriteToken(secToken); Token t = new Token(); t.TokenString = tokenString; t.ObjectUser = u; t.ValidTo = System.DateTime.Now.AddHours(8); t.DeviceName = deviceName; _tokenRepository.Add(t); _tokenRepository.Save(); return(t); }
public IDtoOutObjects Logout(DtoInLogout dtoInLogout) { DtoOutError error = new DtoOutError(); if (TokenTools.Authentication(dtoInLogout.Token, dtoInLogout.DeviceName)) { Token token = TokenTools.getTokenObjectFromString(dtoInLogout.Token); token.IsDeleted = true; tokensRepository.Edit(token); tokensRepository.Save(); var config = new MapperConfiguration(cfg => { cfg.CreateMap <Token, DtoOutToken>(); }); IMapper mapper = config.CreateMapper(); DtoOutToken dtoOutToken = new DtoOutToken(); mapper.Map(token, dtoOutToken); return(dtoOutToken); } else { NotAuthenticatedException ex = new NotAuthenticatedException(); error.Exception = ex; return(error); } }