public async Task <IActionResult> Login(LoginModel model) { if (ModelState.IsValid) { string password = HashProvider.Sha256($"{model.Username}.{model.Password}"); AccountModel account = options.Accounts.FirstOrDefault(a => a.Username == model.Username && a.Password == password); if (account == null) { ModelState.AddModelError(nameof(LoginModel.Username), "No such combination of the username and the password."); return(View()); } ClaimsPrincipal principal = CreatePrincipal(account); AuthenticationProperties authProperties = CreateAuthenticationProperties(); await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, principal, authProperties ); return(RedirectTo()); } return(View()); }
public static async void Authentication(string[] parameters, HttpListenerContext context) { if (parameters.Length != 2) { return; } Action <HttpStatusCode, string> sendResponse = (status, payload) => { byte[] bytes = Encoding.UTF8.GetBytes(payload); context.Response.StatusCode = (int)status; context.Response.ContentLength64 = payload.Length; context.Response.OutputStream.Write(bytes, 0, bytes.Length); }; DataRow accountInfo = await DatabaseManager.DataBase.GetAccountAuth(parameters[0], parameters[1]); if (accountInfo == null) { sendResponse(HttpStatusCode.NotFound, "Not found"); return; } string authToken = HashProvider.Sha256(CryptoProvider.Salt(64u)); await DatabaseManager.DataBase.AccountTokenUpdate(authToken, accountInfo.Read <uint>("id")); string accountTokenB64 = JsonConvert.SerializeObject(new AccountToken { Id = accountInfo.Read <uint>("id"), SteamId = 0ul, Username = accountInfo.Read <string>("username") }).ToBase64(); sendResponse(HttpStatusCode.OK, $"{accountTokenB64}.{authToken}"); }
public static void HandleAccountCreate(Session session, params string[] parameters) { string username = parameters[0]; string salt = HashProvider.GenerateSalt(); string digest = HashProvider.Sha256(parameters[1] + salt); if (DatabaseManager.Authentication.CreateAccount(username, digest, salt)) { Console.WriteLine($"Successfully created account {username}!"); } }
public static void Test() { Console.WriteLine(HashProvider.Sha1("Hello, World!")); Console.WriteLine(HashProvider.Sha256("Hello, World!")); DebugIteration("Threads", 100, () => { Thread t = new Thread(() => Console.WriteLine(HashProvider.Sha1("Hello, World!"))); t.Start(); }); HashFunc hashFunc = new HashFactory().Sha1; DebugIteration("New", 1000, () => hashFunc("Hello, World!")); }