I am using asp.net identy core but am having a problem I am following this I have a form with a text box which I want to pass to my controlle But when I check the authorisation flag on my main page it is not showing as the user has authorisaed I am using an example of what i found on github.
<form asp-controller="Account" asp-action="Login" method="post"><div class="form-group has-feedback"><input type="email" class="form-control" name="email" id="email" placeholder="Email"><span class="glyphicon glyphicon-envelope form-control-feedback"></span></div><div class="form-group has-feedback"><input type="password" class="form-control" id="password" placeholder="Password"><span class="glyphicon glyphicon-lock form-control-feedback"></span></div><div class="row"><div class="col-xs-8"><div class="checkbox icheck"><label><input type="checkbox" name="rememberMe" value="true"> Remember Me<input type="hidden" name="rememberMe" value="false" /></label></div></div><!-- /.col --><div class="col-xs-4"><button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button></div><!-- /.col --></div></form>
Account Controller
public IActionResult Index() { return View(); } private readonly UserManager<IdentityUser> _userManager; private readonly SignInManager<IdentityUser> _signInManager; private readonly IMessageService _messageService; public AccountController(UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager, IMessageService messageService) { this._userManager = userManager; this._signInManager = signInManager; this._messageService = messageService; } public IActionResult Register() { return View(); } [HttpPost] public async Task<IActionResult> Register(string email, string password, string repassword) { if (password != repassword) { ModelState.AddModelError(string.Empty, "Password don't match"); return View(); } var newUser = new IdentityUser { UserName = email, Email = email }; var userCreationResult = await _userManager.CreateAsync(newUser, password); if (!userCreationResult.Succeeded) { foreach (var error in userCreationResult.Errors) ModelState.AddModelError(string.Empty, error.Description); return View(); } await _userManager.AddClaimAsync(newUser, new Claim(ClaimTypes.Role, "Administrator")); var emailConfirmationToken = await _userManager.GenerateEmailConfirmationTokenAsync(newUser); var tokenVerificationUrl = Url.Action("VerifyEmail", "Account", new { id = newUser.Id, token = emailConfirmationToken }, Request.Scheme); await _messageService.Send(email, "Verify your email", $"Click <a href=\"{tokenVerificationUrl}\">here</a> to verify your email"); return Content("Check your email for a verification link"); } public IActionResult TestAuhtorisation() { return View(); } public IActionResult ForgotPassword() { return View(); } public IActionResult ResetPassword(string id, string token) { return View(); } [HttpPost] public async Task<IActionResult> ResetPassword(string id, string token, string password, string repassword) { var user = await _userManager.FindByIdAsync(id); if (user == null) throw new InvalidOperationException(); if (password != repassword) { ModelState.AddModelError(string.Empty, "Passwords do not match"); return View(); } var resetPasswordResult = await _userManager.ResetPasswordAsync(user, token, password); if (!resetPasswordResult.Succeeded) { foreach (var error in resetPasswordResult.Errors) ModelState.AddModelError(string.Empty, error.Description); return View(); } return Content("Password updated"); } public async Task<IActionResult> Logout() { await _signInManager.SignOutAsync(); return Redirect("~/"); } [HttpPost] public async Task<IActionResult> ForgotPassword(string email) { var user = await _userManager.FindByEmailAsync(email); if (user == null) return Content("Check your email for a password reset link"); var passwordResetToken = await _userManager.GeneratePasswordResetTokenAsync(user); var passwordResetUrl = Url.Action("ResetPassword", "Account", new { id = user.Id, token = passwordResetToken }, Request.Scheme); // await _messageService.Send(email, "Password reset", $"Click <a href=\"" + passwordResetUrl + "\">here</a> to reset your password"); return Content("Check your email for a password reset link"); } [HttpPost] public async Task<IActionResult> Login(string email, string password, bool rememberMe) { var user = await _userManager.FindByEmailAsync(email); if (user == null) { ModelState.AddModelError(string.Empty, "Invalid login"); return View(); } if (!user.EmailConfirmed) { ModelState.AddModelError(string.Empty, "Confirm your email first"); return View(); } var passwordSignInResult = await _signInManager.PasswordSignInAsync(user, password, isPersistent: rememberMe, lockoutOnFailure: false); if (!passwordSignInResult.Succeeded) { ModelState.AddModelError(string.Empty, "Invalid login"); return View(); } return Redirect("~/"); } }
My Application Context
public class solitudeDContext : IdentityDbContext<IdentityUser> { public solitudeDContext(DbContextOptions<solitudeDContext> options) : base(options) { } public DbSet<basketheader> BasketHeader { get; set; } public DbSet<basketlines> BasketLines { get; set; } public DbSet<customer> Customer { get; set; } }
My Test Page
@if (User.Identity.IsAuthenticated) {<p>User @User.Identity.Name is signed in. <a href="/Account/Logout">Logout</a> </p> } else { <p>No user is signed in.</p> }