public static ClaimsPrincipal ValidateJWTForUser(string token, Common.Model.User user) { var handler = new JwtSecurityTokenHandler(); var validationParameters = new TokenValidationParameters { ValidIssuer = ISSUER, ValidAudience = AUDIENCE, IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(user.CurrentSessionToken)), RequireExpirationTime = true, ValidateIssuer = true }; try { SecurityToken validatedToken = null; var claims = handler.ValidateToken(token, validationParameters, out validatedToken); return(claims); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("{0}\n {1}", e.Message, e.StackTrace); return(null); } }
public async Task <IActionResult> Post([FromBody] UserRegisterDTO user) { var createdUser = await _userService.CreateUser(user); var token = await _authService.GenerateAccessToken(createdUser.Id, createdUser.UserName, createdUser.Email); Common.Model.User tempUser = new Common.Model.User() { Email = createdUser.Email, UserName = createdUser.UserName }; var code = await _userManager.GenerateEmailConfirmationTokenAsync(tempUser); var callbackUrl = Url.Action( "ConfirmEmail", "Register", new { userId = createdUser.Id, code = code }, protocol: HttpContext.Request.Scheme); try { await emailService.SendEmailAsync(createdUser.Email, "Confirm your account", $"Подтвердите регистрацию, перейдя по ссылке: <a href='{callbackUrl}'>link</a>"); } catch (Exception e) { Console.WriteLine(e); } var result = new AuthUserDTO { User = createdUser, Token = token }; return(CreatedAtAction("GetById", "users", new { id = createdUser.Id }, result)); }
public static void Main(string[] args) { try { //Configure the Logger Logging.ConfigureLogger(); //Load the configuration from appsettings.json Config.LoadAppSettings(); //load the cities list CitiesConfig.LoadCitiesList(); //Initialize DB using (BSMContext context = new BSMContext(Config.DBConnection)) { if (context.Database.EnsureCreated()) { Log.Information("Database schemas initialized"); } var adminuser = context.Users.FirstOrDefault(u => u.LoginID.Equals("admin", StringComparison.OrdinalIgnoreCase)); if (adminuser == null) { adminuser = new Common.Model.User(); adminuser.LoginID = "admin"; adminuser.Name = "admin"; adminuser.Title = "Administrator"; adminuser.IsAdmin = true; PasswordHasher <User> hasher = new PasswordHasher <User>(); string hashedPass = hasher.HashPassword(adminuser, "admin"); adminuser.Password = hashedPass; adminuser.PasswordChangedAt = DateTime.Now; adminuser.CreatedAt = DateTime.Now; context.Users.Add(adminuser); context.SaveChanges(); Log.Information("admin user is created in system"); } var alertsetting = context.AlertSettings.FirstOrDefault(); if (alertsetting == null) { alertsetting = new AlertSetting(); alertsetting.TiltThreshold = null; alertsetting.SkewingThreshold = null; alertsetting.SpeedThreshold = null; alertsetting.TempThreshold = null; context.AlertSettings.Add(alertsetting); context.SaveChanges(); Log.Information("alert settings is configured"); } Log.Information("Database initialized: {0}", context.DataSourceString); } //start up the udp server if (Config.UdpEnabled) { UdpServer udpserver = new UdpServer(); udpserver.Init(); } else { Log.Information("UDP server is disabled, will not start up to listen on udp port"); } //Start the web server host (include websocket) CreateWebHostBuilder(args).Build().Run(); } catch (Exception ex) { Log.Error(ex, "Error when startup the service"); } }