Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Remember the email a User signs in with

$
0
0

I have a login page in Asp.Net Core with two simple fields like so:

<form method="post" asp-action="SignIn"
        asp-route-returnUrl="@Context.Request.Query["returnUrl"]"><img class="card-img-top user-image" src="~/images/user.svg" alt="Card image cap">
    @if (@Model.SignInError)
    {<div class="form-group row messagebox Failure"><div class="col-sm-2 imageContainer"><img class="warning-image" src="https://cdn1.iconfinder.com/data/icons/smallicons-controls/32/614338-.svg-512.png" alt="Card image cap"></div><div class="col-sm-10"><p class="failureText">@Model.ErrorMessageLine1</p><p class="failureText">@Model.ErrorMessageLine2</p></div></div>
    }<div class="form-group row"><div class="col"><input type="email" class="form-control" asp-for="Email" placeholder="Email"/></div></div><div class="form-group row"><div class="col"><input type="password" class="form-control" asp-for="Password" placeholder="Password"/></div></div><div class="form-group row"><div class="col"><button class="btn btn-success" type="submit" name="button" style="width: 100%;">Login</button></div></div></form>

I would like to remember the email the user succesfully signs in with so that the next time the user navigates to the login page it is filled in for them automatically and they only have to type their password. How is this possible? This is how I login a User:

[Route("signin")]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SignIn(SignInViewModel model, string returnUrl = null)
{
    if (ModelState.IsValid && await _userService.ValidateCredentials(model.Email, model.Password))
    {
        await SignInUser(new User
        {
            Email = model.Email
        });
        if (returnUrl != null)
        {
            return Redirect(returnUrl);
        }
        return RedirectToAction("Index", "Home");
    }
    return View(new SignInViewModel
    {
        ErrorMessageLine1 = "Sign in Failed.",
        ErrorMessageLine2 = await _userService.ReturnErrorMessage(),
        SignInError = true
    });
}
private async Task SignInUser(User user)
{
    var claims = new List<Claim>
    {
        new Claim(ClaimTypes.NameIdentifier, user.Email),
        new Claim("name", user.Email)
    };
    var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme, "name", null);
    var principal = new ClaimsPrincipal(identity);
    await HttpContext.SignInAsync(principal);
}

Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>