public async Task <IActionResult> RegisterUserAsync([FromForm] string name, [FromForm] string password) { if (!CustomizationOptions.AllowUserRegistration) { return(NotFound()); } if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(password)) { return(UnprocessableEntity()); } if (await DbContext.Users.AnyAsync(x => x.Name.ToUpper() == name.ToUpper())) { return(Conflict("Username already taken!")); } string passwordHash = HashingService.HashString(password); var user = new User(name, passwordHash); DbContext.Users.Add(user); await DbContext.SaveChangesAsync(); var userInfo = UserService.GetUserInfo(user); return(Ok(userInfo)); }
public async Task <IActionResult> GetWalletAsync([FromRoute] string name, [FromRoute] string password) { string passwordHash = HashService.HashString(password); var user = await DbContext.Users.FirstOrDefaultAsync(x => x.Name == name && x.PasswordHash == passwordHash); if (user == null) { return(Unauthorized()); } long totalPlotMinutes = await DbContext.Miners .Where(x => x.OwnerId == user.Id) .SumAsync(x => x.PlotMinutes); var wallet = await WalletService.GetWalletFractionAsync(totalPlotMinutes); return(Ok(wallet)); }
protected override async Task <AuthenticateResult> HandleAuthenticateAsync() { if (!Request.Headers.TryGetValue("Authorization", out var header) || !AuthenticationHeaderValue.TryParse(header, out var authHeader) || authHeader.Scheme != Scheme.Name) { return(AuthenticateResult.NoResult()); } byte[] authBytes = Convert.FromBase64String(authHeader.Parameter); string userAndPass = Encoding.UTF8.GetString(authBytes); string[] parts = userAndPass.Split(':'); if (parts.Length != 2) { return(AuthenticateResult.Fail($"Invalid {Scheme.Name} authentication header")); } string username = parts[0]; string password = parts[1]; string passwordHash = HashingService.HashString(password); var user = await DbContext.Users.FirstOrDefaultAsync(x => x.Name == username && x.PasswordHash == passwordHash); if (user == null) { return(AuthenticateResult.Fail("Invalid username or password")); } var claims = new[] { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), }; var identity = new ClaimsIdentity(claims, Scheme.Name); var principal = new ClaimsPrincipal(identity); var ticket = new AuthenticationTicket(principal, Scheme.Name); return(AuthenticateResult.Success(ticket)); }