1
0

Added custom redirection url after authorization

This commit is contained in:
Michael Gordeev
2020-04-04 21:30:46 +03:00
parent d64d690f48
commit a1edca63e7
3 changed files with 14 additions and 5 deletions
@@ -10,6 +10,7 @@ using MyWebsite.Models;
using MyWebsite.Models.Databases;
using MyWebsite.ViewModels;
#pragma warning disable CA1054 // Uri parameters should not be strings
namespace MyWebsite.Controllers
{
[Authorize]
@@ -22,15 +23,15 @@ namespace MyWebsite.Controllers
[AllowAnonymous]
[HttpGet]
public IActionResult Login() =>
View(new CredentialViewModel(Database));
public IActionResult Login(string ReturnUrl) =>
View(new CredentialViewModel(Database, ReturnUrl));
[AllowAnonymous]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(CredentialViewModel model)
{
if (!ModelState.IsValid)
if (!ModelState.IsValid || model == null)
{
ModelState.AddModelError("Authorization error", "Invalid data");
return View(new CredentialViewModel(Database, model));
@@ -52,7 +53,7 @@ namespace MyWebsite.Controllers
ClaimsIdentity id = new ClaimsIdentity(new Claim[] { claim }, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(id)).ConfigureAwait(false);
return RedirectToAction("Index", "Admin");
return Redirect(model.ReturnUrl ?? "/Admin");
}
public async Task<IActionResult> Logout()
@@ -1,13 +1,19 @@
using MyWebsite.Models;
using MyWebsite.Models.Databases;
#pragma warning disable CA1054 // Uri parameters should not be strings
#pragma warning disable CA1056 // Uri properties should not be strings
namespace MyWebsite.ViewModels
{
public class CredentialViewModel : ViewModelBase
{
public CredentialModel Credential { get; set; }
public string ReturnUrl { get; set; }
public CredentialViewModel(DatabaseContext context) : base(context) { }
public CredentialViewModel(DatabaseContext context, string returnUrl) : base(context) =>
ReturnUrl = returnUrl;
public CredentialViewModel() : base(null) { }
public CredentialViewModel(DatabaseContext context, CredentialViewModel model) : base(context) =>
Credential = model?.Credential;
+3 -1
View File
@@ -10,6 +10,7 @@
<article>
<form asp-action="Login" asp-antiforgery="true">
<div asp-validation-summary="All" class="validation-error"></div>
<input hidden asp-for="ReturnUrl" />
<div>
<label asp-for="Credential.Email">E-mail</label>
<input type="email" asp-for="Credential.Email" />
@@ -20,7 +21,8 @@
<input type="password" asp-for="Credential.Password" />
<span asp-validation-for="Credential.Password"></span>
</div>
<input class="btn" style="margin-top:10px" type="submit" value="Login" />
<input type="submit" value="Login" />
</form>
</article>