Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 48256a8

Browse files
Add claims in asp.net core
1 parent 7f1da5e commit 48256a8

File tree

6 files changed

+70
-38
lines changed

6 files changed

+70
-38
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.AspNetCore.Identity;
2+
using Microsoft.Extensions.Options;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Security.Claims;
7+
using System.Threading.Tasks;
8+
using Webgentle.BookStore.Models;
9+
10+
namespace Webgentle.BookStore.Helpers
11+
{
12+
public class ApplicationUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>
13+
{
14+
public ApplicationUserClaimsPrincipalFactory(UserManager<ApplicationUser> userManager,
15+
RoleManager<IdentityRole> roleManager, IOptions<IdentityOptions> options)
16+
:base(userManager, roleManager, options)
17+
{
18+
}
19+
20+
protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user)
21+
{
22+
var identity = await base.GenerateClaimsAsync(user);
23+
identity.AddClaim(new Claim("UserFirstName", user.FirstName ?? ""));
24+
identity.AddClaim(new Claim("UserLastName", user.LastName ?? ""));
25+
return identity;
26+
}
27+
}
28+
}

‎Webgentle.BookStore/Webgentle.BookStore/Models/SignUpUserModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ namespace Webgentle.BookStore.Models
99
public class SignUpUserModel
1010
{
1111
[Required(ErrorMessage = "Please enter your first name")]
12+
[Display(Name = "First name")]
1213
public string FirstName { get; set; }
14+
15+
[Display(Name = "Last name")]
1316
public string LastName { get; set; }
1417

1518
[Required(ErrorMessage = "Please enter your email")]

‎Webgentle.BookStore/Webgentle.BookStore/Startup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.Extensions.FileProviders;
1414
using Microsoft.Extensions.Hosting;
1515
using Webgentle.BookStore.Data;
16+
using Webgentle.BookStore.Helpers;
1617
using Webgentle.BookStore.Models;
1718
using Webgentle.BookStore.Repository;
1819

@@ -67,6 +68,8 @@ public void ConfigureServices(IServiceCollection services)
6768
services.AddSingleton<IMessageRepository, MessageRepository>();
6869
services.AddScoped<IAccountRepository, AccountRepository>();
6970

71+
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, ApplicationUserClaimsPrincipalFactory>();
72+
7073
services.Configure<NewBookAlertConfig>("InternalBook", _configuration.GetSection("NewBookAlert"));
7174
services.Configure<NewBookAlertConfig>("ThirdPartyBook", _configuration.GetSection("ThirdPartyBook"));
7275
}

‎Webgentle.BookStore/Webgentle.BookStore/Views/Account/Login.cshtml

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,42 @@
2727
</environment>
2828
</head>
2929
<body>
30-
31-
<h4>Sign In</h4>
32-
<hr />
33-
<div class="container">
34-
<div class="row">
35-
<divclass="col-md-4">
36-
<formmethod="post">
37-
<div asp-validation-summary="ModelOnly"class="text-danger"></div>
38-
<divclass="form-group">
39-
<label asp-for="Email" class="control-label"></label>
40-
<input asp-for="Email" class="form-control" />
41-
<spanasp-validation-for="Email"class="text-danger"></span>
42-
</div>
43-
<divclass="form-group">
44-
<label asp-for="Password" class="control-label"></label>
45-
<input asp-for="Password" class="form-control" />
46-
<spanasp-validation-for="Password"class="text-danger"></span>
47-
</div>
48-
<div class="form-group form-check">
49-
<label class="form-check-label">
50-
<inputclass="form-check-input"asp-for="RememberMe" /> @Html.DisplayNameFor(model => model.RememberMe)
51-
</label>
52-
</div>
53-
<divclass="form-group">
54-
<inputtype="submit"value="Login"class="btn btn-primary" />
55-
</div>
56-
</form>
57-
</div>
58-
<div class="col-md-8">
59-
<imgclass="img-fluid"src="~/images/login.png" />
30+
<divclass="container">
31+
<h4class="display-4">Sign In</h4>
32+
<hr />
33+
<div class="row">
34+
<div class="col-md-4">
35+
<formmethod="post">
36+
<divasp-validation-summary="ModelOnly"class="text-danger"></div>
37+
<div class="form-group">
38+
<labelasp-for="Email"class="control-label"></label>
39+
<input asp-for="Email" class="form-control" />
40+
<span asp-validation-for="Email" class="text-danger"></span>
41+
</div>
42+
<divclass="form-group">
43+
<labelasp-for="Password"class="control-label"></label>
44+
<input asp-for="Password" class="form-control" />
45+
<span asp-validation-for="Password" class="text-danger"></span>
46+
</div>
47+
<divclass="form-group form-check">
48+
<label class="form-check-label">
49+
<input class="form-check-input"asp-for="RememberMe" /> @Html.DisplayNameFor(model => model.RememberMe)
50+
</label>
51+
</div>
52+
<divclass="form-group">
53+
<inputtype="submit"value="Login"class="btn btn-primary" />
54+
</div>
55+
</form>
56+
</div>
57+
<divclass="col-md-8">
58+
<img class="img-fluid"src="~/images/login.png" />
59+
</div>
6060
</div>
61-
</div>
6261

63-
<div>
64-
<p>Click <a asp-action="Signup" asp-controller="Account">here</a> to signup</p>
62+
<div>
63+
<p>Click <a asp-action="Signup" asp-controller="Account">here</a> to signup</p>
64+
</div>
6565
</div>
66-
</div>
6766

6867
</body>
6968
</html>

‎Webgentle.BookStore/Webgentle.BookStore/Views/Account/Signup.cshtml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
</head>
2929
<body>
3030
<div class="container">
31-
32-
<h4>Sign up</h4>
31+
<h4 class="display-4">Sign up</h4>
3332
<hr />
3433
<div class="row">
3534
<div class="col-md-4">
@@ -62,7 +61,7 @@
6261
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
6362
</div>
6463
<div class="form-group">
65-
<input type="submit" value="Create" class="btn btn-primary" />
64+
<input type="submit" value="Signup" class="btn btn-primary" />
6665
</div>
6766
</form>
6867
</div>

‎Webgentle.BookStore/Webgentle.BookStore/Views/Shared/_LoginInfo.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{
66
<li class="nav-item dropdown">
77
<a class="nav-link dropdown-toggle text-dark" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
8-
Hello @User.Identity.Name
8+
Hello @(User.FindFirst("UserFirstName").Value+""+User.FindFirst("UserLastName").Value)
99
</a>
1010
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
1111
<a class="dropdown-item" asp-action="Logout" asp-controller="Account">Logout</a>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /