Skip to content

Commit 16a9fc6

Browse files
authored
Added Login Control - 0.4.0-preview2 (#44)
Added Login Control - 0.4.0-preview2
2 parents da2d641 + 467dc7f commit 16a9fc6

File tree

62 files changed

+1236
-128
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1236
-128
lines changed

samples/AfterBlazorClientSide/Startup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.AspNetCore.Components.Authorization;
12
using Microsoft.AspNetCore.Components.Builder;
23
using Microsoft.Extensions.DependencyInjection;
34

@@ -7,6 +8,7 @@ public class Startup
78
{
89
public void ConfigureServices(IServiceCollection services)
910
{
11+
services.AddScoped<AuthenticationStateProvider, StaticAuthStateProvider>();
1012
}
1113

1214
public void Configure(IComponentsApplicationBuilder app)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Security.Claims;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Components.Authorization;
7+
8+
namespace AfterBlazorClientSide
9+
{
10+
public class StaticAuthStateProvider : AuthenticationStateProvider
11+
{
12+
13+
private static ClaimsPrincipal _user = new ClaimsPrincipal();
14+
15+
public static void Login(string name)
16+
{
17+
18+
var identity = new ClaimsIdentity(new[]
19+
{
20+
new Claim(ClaimTypes.Name, name),
21+
}, "static authentication");
22+
23+
_user = new ClaimsPrincipal(identity);
24+
25+
}
26+
27+
public static void Logout()
28+
{
29+
30+
_user = new ClaimsPrincipal();
31+
32+
}
33+
34+
public override Task<AuthenticationState> GetAuthenticationStateAsync()
35+
{
36+
37+
return Task.FromResult(new AuthenticationState(_user));
38+
39+
}
40+
}
41+
}

samples/AfterBlazorServerSide/Pages/ComponentList.razor

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@
4141
<div class="col-md=3">
4242
<h3>Login Controls</h3>
4343
<ul>
44+
<li><a href="/ControlSamples/Login">Login</a></li>
45+
<li>ChangePassword</li>
46+
<li><a href="/ControlSamples/LoginName">LoginName</a></li>
47+
<li><a href="/ControlSamples/LoginStatusAuthenticated">LoginStatus - Authenticated</a></li>
48+
<li><a href="/ControlSamples/LoginStatusNotAuthenticated">LoginStatus - Not Authenticated</a></li>
49+
<li>LoginView</li>
50+
<li>PasswordRecovery</li>
4451
</ul>
4552
</div>
46-
4753
</div>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@page "/ControlSamples/LoginName"
2+
3+
<h3>LoginName</h3>
4+
@using BlazorWebFormsComponents.Validations;
5+
@using BlazorWebFormsComponents.LoginControls;
6+
7+
8+
<LoginName BackColor="@("#FFFFFF")"
9+
FormatString="The name is {0}!" />
10+
11+
12+
13+
@code
14+
{
15+
protected override void OnInitialized()
16+
{
17+
18+
StaticAuthStateProvider.Login("James Bond");
19+
20+
base.OnInitialized();
21+
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@page "/ControlSamples/Login"
2+
3+
<h3>Login</h3>
4+
@using BlazorWebFormsComponents.Validations;
5+
@using BlazorWebFormsComponents.LoginControls;
6+
7+
8+
<Login InstructionText="How to Login!!">
9+
<ChildContent>
10+
<InstructionTextStyle BackColor="@("#FF0000")"
11+
CssClass="sdd" />
12+
<ValidatorTextStyle ForeColor="@("#0000ff")" />
13+
</ChildContent>
14+
</Login>
15+
16+
17+
18+
@code
19+
{
20+
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@page "/ControlSamples/LoginStatusAuthenticated"
2+
3+
@using BlazorWebFormsComponents.LoginControls;
4+
@using static BlazorWebFormsComponents.Enums.LogoutAction;
5+
6+
<h3>LoginStatus Authenticated</h3>
7+
8+
9+
<LoginStatus BackColor="@("#FF00FF")"
10+
LogoutPageUrl="LoginPageUrl"
11+
LogoutAction="Redirect" />
12+
13+
14+
15+
@code
16+
{
17+
protected override void OnInitialized()
18+
{
19+
20+
StaticAuthStateProvider.Login("James Bond");
21+
22+
base.OnInitialized();
23+
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@page "/ControlSamples/LoginStatusNotAuthenticated"
2+
3+
@using BlazorWebFormsComponents.LoginControls;
4+
5+
<h3>LoginStatus Not Authenticated</h3>
6+
7+
8+
<LoginStatus BackColor="@("#FF00FF")"
9+
LoginPageUrl="LoginPageUrl"/>
10+
11+
12+
@code
13+
{
14+
protected override void OnInitialized()
15+
{
16+
17+
StaticAuthStateProvider.Logout();
18+
19+
base.OnInitialized();
20+
21+
}
22+
}

samples/AfterBlazorServerSide/Pages/ControlSamples/_Imports.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33

44
@using static BlazorWebFormsComponents.Enums.RepeatLayout
55
@using static BlazorWebFormsComponents.Enums.ValidationSummaryDisplayMode
6+
@using static BlazorWebFormsComponents.Enums.ButtonType

samples/AfterBlazorServerSide/Startup.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading.Tasks;
55
using Microsoft.AspNetCore.Builder;
66
using Microsoft.AspNetCore.Components;
7+
using Microsoft.AspNetCore.Components.Authorization;
78
using Microsoft.AspNetCore.Hosting;
89
using Microsoft.AspNetCore.HttpsPolicy;
910
using Microsoft.Extensions.Configuration;
@@ -27,10 +28,11 @@ public void ConfigureServices(IServiceCollection services)
2728
{
2829
services.AddRazorPages();
2930
services.AddServerSideBlazor();
30-
}
31+
services.AddScoped<AuthenticationStateProvider, StaticAuthStateProvider>();
32+
}
3133

32-
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
33-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
34+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
35+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3436
{
3537
if (env.IsDevelopment())
3638
{
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Security.Claims;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Components.Authorization;
7+
8+
namespace AfterBlazorServerSide
9+
{
10+
public class StaticAuthStateProvider : AuthenticationStateProvider
11+
{
12+
13+
private static ClaimsPrincipal _user = new ClaimsPrincipal();
14+
15+
public static void Login(string name)
16+
{
17+
18+
var identity = new ClaimsIdentity(new[]
19+
{
20+
new Claim(ClaimTypes.Name, name),
21+
}, "static authentication");
22+
23+
_user = new ClaimsPrincipal(identity);
24+
25+
}
26+
27+
public static void Logout()
28+
{
29+
30+
_user = new ClaimsPrincipal();
31+
32+
}
33+
34+
public override Task<AuthenticationState> GetAuthenticationStateAsync()
35+
{
36+
37+
return Task.FromResult(new AuthenticationState(_user));
38+
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)