I'm in the stage of "Create an authentication cookie" and this is Microsoft implementation:
public async Task<IActionResult> OnPostAsync(string returnUrl = null){ ReturnUrl = returnUrl; if (ModelState.IsValid) { // Use Input.Email and Input.Password to authenticate the user // with your custom authentication logic. // // For demonstration purposes, the sample validates the user // on the email address maria.rodriguez@contoso.com with // any password that passes model validation. var user = await AuthenticateUser(Input.Email, Input.Password); if (user == null) { ModelState.AddModelError(string.Empty, "Invalid login attempt."); return Page(); } ... too much code, I can't add it }; await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); _logger.LogInformation("User {Email} logged in at {Time}.", user.Email, DateTime.UtcNow); return LocalRedirect(Url.GetLocalUrl(returnUrl)); } // Something failed. Redisplay the form. return Page();}
public async Task<IActionResult> OnPostAsync(string returnUrl = null){ ReturnUrl = returnUrl; if (ModelState.IsValid) { // Use Input.Email and Input.Password to authenticate the user // with your custom authentication logic. // // For demonstration purposes, the sample validates the user // on the email address maria.rodriguez@contoso.com with // any password that passes model validation. var user = await AuthenticateUser(Input.Email, Input.Password); if (user == null) { ModelState.AddModelError(string.Empty, "Invalid login attempt."); return Page(); } ... too much code, I can't add it }; await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); _logger.LogInformation("User {Email} logged in at {Time}.", user.Email, DateTime.UtcNow); return LocalRedirect(Url.GetLocalUrl(returnUrl)); } // Something failed. Redisplay the form. return Page();}
The problems are: (1) I don't use razor pages. I'm using React and backend. (2) My plan is: The user to login from the website -> Sends a POST request with HTTP to the backend which uses my controller method -> Then I want to use this method to create the cookie -> Send it back to the frontend. I don't know what I need to change and how.