Skip to content

Commit 42aa1e7

Browse files
committed
feat: added user and roles controller and admin panels are hidden from users
1 parent 6f31970 commit 42aa1e7

34 files changed

+1475
-121
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using Blog.Data;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.Identity;
5+
using Microsoft.AspNetCore.Identity.UI;
6+
using Microsoft.EntityFrameworkCore;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.DependencyInjection;
9+
10+
[assembly: HostingStartup(typeof(Blog.Areas.Identity.IdentityHostingStartup))]
11+
namespace Blog.Areas.Identity
12+
{
13+
public class IdentityHostingStartup : IHostingStartup
14+
{
15+
public void Configure(IWebHostBuilder builder)
16+
{
17+
builder.ConfigureServices((context, services) => {
18+
});
19+
}
20+
}
21+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
@page
2+
@model LoginModel
3+
4+
@{
5+
ViewData["Title"] = "Log in";
6+
}
7+
8+
<h1>@ViewData["Title"]</h1>
9+
<div class="row">
10+
<div class="col-md-4">
11+
<section>
12+
<form id="account" method="post">
13+
<h4>Use a local account to log in.</h4>
14+
<hr />
15+
<div asp-validation-summary="All" class="text-danger"></div>
16+
<div class="form-group">
17+
<label asp-for="Input.Email"></label>
18+
<input asp-for="Input.Email" class="form-control" />
19+
<span asp-validation-for="Input.Email" class="text-danger"></span>
20+
</div>
21+
<div class="form-group">
22+
<label asp-for="Input.Password"></label>
23+
<input asp-for="Input.Password" class="form-control" />
24+
<span asp-validation-for="Input.Password" class="text-danger"></span>
25+
</div>
26+
<div class="form-group">
27+
<div class="checkbox">
28+
<label asp-for="Input.RememberMe">
29+
<input asp-for="Input.RememberMe" />
30+
@Html.DisplayNameFor(m => m.Input.RememberMe)
31+
</label>
32+
</div>
33+
</div>
34+
<div class="form-group">
35+
<button type="submit" class="btn btn-primary">Log in</button>
36+
</div>
37+
<div class="form-group">
38+
<p>
39+
<a id="forgot-password" asp-page="./ForgotPassword">Forgot your password?</a>
40+
</p>
41+
<p>
42+
<a asp-page="./Register" asp-route-returnUrl="@Model.ReturnUrl">Register as a new user</a>
43+
</p>
44+
</div>
45+
</form>
46+
</section>
47+
</div>
48+
<div class="col-md-6 col-md-offset-2">
49+
<section>
50+
<h4>Use another service to log in.</h4>
51+
<hr />
52+
@{
53+
if ((Model.ExternalLogins?.Count ?? 0) == 0)
54+
{
55+
<div>
56+
<p>
57+
There are no external authentication services configured. See <a href="https://go.microsoft.com/fwlink/?LinkID=532715">this article</a>
58+
for details on setting up this ASP.NET application to support logging in via external services.
59+
</p>
60+
</div>
61+
}
62+
else
63+
{
64+
<form id="external-account" asp-page="./ExternalLogin" asp-route-returnUrl="@Model.ReturnUrl" method="post" class="form-horizontal">
65+
<div>
66+
<p>
67+
@foreach (var provider in Model.ExternalLogins)
68+
{
69+
<button type="submit" class="btn btn-primary" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button>
70+
}
71+
</p>
72+
</div>
73+
</form>
74+
}
75+
}
76+
</section>
77+
</div>
78+
</div>
79+
80+
@section Scripts {
81+
<partial name="_ValidationScriptsPartial" />
82+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Text.Encodings.Web;
6+
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Authorization;
8+
using Microsoft.AspNetCore.Authentication;
9+
using Microsoft.AspNetCore.Identity;
10+
using Microsoft.AspNetCore.Identity.UI.Services;
11+
using Microsoft.AspNetCore.Mvc;
12+
using Microsoft.AspNetCore.Mvc.RazorPages;
13+
using Microsoft.Extensions.Logging;
14+
15+
namespace Blog.Areas.Identity.Pages.Account
16+
{
17+
[AllowAnonymous]
18+
public class LoginModel : PageModel
19+
{
20+
private readonly UserManager<IdentityUser> _userManager;
21+
private readonly SignInManager<IdentityUser> _signInManager;
22+
private readonly ILogger<LoginModel> _logger;
23+
24+
public LoginModel(SignInManager<IdentityUser> signInManager,
25+
ILogger<LoginModel> logger,
26+
UserManager<IdentityUser> userManager)
27+
{
28+
_userManager = userManager;
29+
_signInManager = signInManager;
30+
_logger = logger;
31+
}
32+
33+
[BindProperty]
34+
public InputModel Input { get; set; }
35+
36+
public IList<AuthenticationScheme> ExternalLogins { get; set; }
37+
38+
public string ReturnUrl { get; set; }
39+
40+
[TempData]
41+
public string ErrorMessage { get; set; }
42+
43+
public class InputModel
44+
{
45+
[Required]
46+
[EmailAddress]
47+
public string Email { get; set; }
48+
49+
[Required]
50+
[DataType(DataType.Password)]
51+
public string Password { get; set; }
52+
53+
[Display(Name = "Remember me?")]
54+
public bool RememberMe { get; set; }
55+
}
56+
57+
public async Task OnGetAsync(string returnUrl = null)
58+
{
59+
if (!string.IsNullOrEmpty(ErrorMessage))
60+
{
61+
ModelState.AddModelError(string.Empty, ErrorMessage);
62+
}
63+
64+
returnUrl = returnUrl ?? Url.Content("~/");
65+
66+
// Clear the existing external cookie to ensure a clean login process
67+
await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
68+
69+
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
70+
71+
ReturnUrl = returnUrl;
72+
}
73+
74+
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
75+
{
76+
returnUrl = returnUrl ?? Url.Content("~/");
77+
78+
if (ModelState.IsValid)
79+
{
80+
// This doesn't count login failures towards account lockout
81+
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
82+
var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
83+
if (result.Succeeded)
84+
{
85+
_logger.LogInformation("User logged in.");
86+
return LocalRedirect(returnUrl);
87+
}
88+
if (result.RequiresTwoFactor)
89+
{
90+
return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
91+
}
92+
if (result.IsLockedOut)
93+
{
94+
_logger.LogWarning("User account locked out.");
95+
return RedirectToPage("./Lockout");
96+
}
97+
else
98+
{
99+
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
100+
return Page();
101+
}
102+
}
103+
104+
// If we got this far, something failed, redisplay form
105+
return Page();
106+
}
107+
}
108+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@page
2+
@model LogoutModel
3+
@{
4+
ViewData["Title"] = "Log out";
5+
}
6+
7+
<header>
8+
<h1>@ViewData["Title"]</h1>
9+
<p>You have successfully logged out of the application.</p>
10+
</header>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Authorization;
6+
using Microsoft.AspNetCore.Identity;
7+
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.AspNetCore.Mvc.RazorPages;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace Blog.Areas.Identity.Pages.Account
12+
{
13+
[AllowAnonymous]
14+
public class LogoutModel : PageModel
15+
{
16+
private readonly SignInManager<IdentityUser> _signInManager;
17+
private readonly ILogger<LogoutModel> _logger;
18+
19+
public LogoutModel(SignInManager<IdentityUser> signInManager, ILogger<LogoutModel> logger)
20+
{
21+
_signInManager = signInManager;
22+
_logger = logger;
23+
}
24+
25+
public void OnGet()
26+
{
27+
}
28+
29+
public async Task<IActionResult> OnPost(string returnUrl = null)
30+
{
31+
await _signInManager.SignOutAsync();
32+
_logger.LogInformation("User logged out.");
33+
if (returnUrl != null)
34+
{
35+
return LocalRedirect(returnUrl);
36+
}
37+
else
38+
{
39+
return RedirectToPage();
40+
}
41+
}
42+
}
43+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
@page
2+
@model RegisterModel
3+
@{
4+
ViewData["Title"] = "Register";
5+
}
6+
7+
<h1>@ViewData["Title"]</h1>
8+
9+
<div class="row">
10+
<div class="col-md-4">
11+
<form asp-route-returnUrl="@Model.ReturnUrl" method="post">
12+
<h4>Create a new account.</h4>
13+
<hr />
14+
<div asp-validation-summary="All" class="text-danger"></div>
15+
<div class="form-group">
16+
<label asp-for="Input.Email"></label>
17+
<input asp-for="Input.Email" class="form-control" />
18+
<span asp-validation-for="Input.Email" class="text-danger"></span>
19+
</div>
20+
<div class="form-group">
21+
<label asp-for="Input.Password"></label>
22+
<input asp-for="Input.Password" class="form-control" />
23+
<span asp-validation-for="Input.Password" class="text-danger"></span>
24+
</div>
25+
<div class="form-group">
26+
<label asp-for="Input.ConfirmPassword"></label>
27+
<input asp-for="Input.ConfirmPassword" class="form-control" />
28+
<span asp-validation-for="Input.ConfirmPassword" class="text-danger"></span>
29+
</div>
30+
<button type="submit" class="btn btn-primary">Register</button>
31+
</form>
32+
</div>
33+
<div class="col-md-6 col-md-offset-2">
34+
<section>
35+
<h4>Use another service to register.</h4>
36+
<hr />
37+
@{
38+
if ((Model.ExternalLogins?.Count ?? 0) == 0)
39+
{
40+
<div>
41+
<p>
42+
There are no external authentication services configured. See <a href="https://go.microsoft.com/fwlink/?LinkID=532715">this article</a>
43+
for details on setting up this ASP.NET application to support logging in via external services.
44+
</p>
45+
</div>
46+
}
47+
else
48+
{
49+
<form id="external-account" asp-page="./ExternalLogin" asp-route-returnUrl="@Model.ReturnUrl" method="post" class="form-horizontal">
50+
<div>
51+
<p>
52+
@foreach (var provider in Model.ExternalLogins)
53+
{
54+
<button type="submit" class="btn btn-primary" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button>
55+
}
56+
</p>
57+
</div>
58+
</form>
59+
}
60+
}
61+
</section>
62+
</div>
63+
</div>
64+
65+
@section Scripts {
66+
<partial name="_ValidationScriptsPartial" />
67+
}

0 commit comments

Comments
 (0)