Skip to content

Commit 4afd671

Browse files
committed
Add project files.
1 parent d6ad54b commit 4afd671

Some content is hidden

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

44 files changed

+2350
-0
lines changed

BlazorServerCaptureUser.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33220.173
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorServerCaptureUser", "BlazorServerCaptureUser\BlazorServerCaptureUser.csproj", "{021B9F11-9000-49E1-8521-40C80B1D667C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{021B9F11-9000-49E1-8521-40C80B1D667C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{021B9F11-9000-49E1-8521-40C80B1D667C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{021B9F11-9000-49E1-8521-40C80B1D667C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{021B9F11-9000-49E1-8521-40C80B1D667C}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {BEC90ADA-F363-4EDC-BE1B-A26BDDB69C9C}
24+
EndGlobalSection
25+
EndGlobal

BlazorServerCaptureUser/App.razor

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<CascadingAuthenticationState>
2+
<Router AppAssembly="@typeof(App).Assembly">
3+
<Found Context="routeData">
4+
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
5+
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
6+
</Found>
7+
<NotFound>
8+
<PageTitle>Not found</PageTitle>
9+
<LayoutView Layout="@typeof(MainLayout)">
10+
<p role="alert">Sorry, there's nothing at this address.</p>
11+
</LayoutView>
12+
</NotFound>
13+
</Router>
14+
</CascadingAuthenticationState>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@page
2+
@using Microsoft.AspNetCore.Identity
3+
@attribute [IgnoreAntiforgeryToken]
4+
@inject SignInManager<IdentityUser> SignInManager
5+
@functions {
6+
public async Task<IActionResult> OnPost()
7+
{
8+
if (SignInManager.IsSignedIn(User))
9+
{
10+
await SignInManager.SignOutAsync();
11+
}
12+
13+
return Redirect("~/");
14+
}
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@using Microsoft.AspNetCore.Identity
2+
@inject SignInManager<IdentityUser> SignInManager
3+
@inject UserManager<IdentityUser> UserManager
4+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
5+
6+
<ul class="navbar-nav">
7+
@if (SignInManager.IsSignedIn(User))
8+
{
9+
<li class="nav-item">
10+
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @User.Identity?.Name!</a>
11+
</li>
12+
<li class="nav-item">
13+
<form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="/" method="post">
14+
<button type="submit" class="nav-link btn btn-link text-dark">Logout</button>
15+
</form>
16+
</li>
17+
}
18+
else
19+
{
20+
<li class="nav-item">
21+
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Register">Register</a>
22+
</li>
23+
<li class="nav-item">
24+
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>
25+
</li>
26+
}
27+
</ul>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Microsoft.AspNetCore.Components;
2+
using Microsoft.AspNetCore.Components.Authorization;
3+
using Microsoft.AspNetCore.Components.Server;
4+
using Microsoft.AspNetCore.Identity;
5+
using Microsoft.Extensions.Options;
6+
using System.Security.Claims;
7+
8+
namespace BlazorServerCaptureUser.Areas.Identity;
9+
public class RevalidatingIdentityAuthenticationStateProvider<TUser>
10+
: RevalidatingServerAuthenticationStateProvider where TUser : class
11+
{
12+
private readonly IServiceScopeFactory _scopeFactory;
13+
private readonly IdentityOptions _options;
14+
15+
public RevalidatingIdentityAuthenticationStateProvider(
16+
ILoggerFactory loggerFactory,
17+
IServiceScopeFactory scopeFactory,
18+
IOptions<IdentityOptions> optionsAccessor)
19+
: base(loggerFactory)
20+
{
21+
_scopeFactory = scopeFactory;
22+
_options = optionsAccessor.Value;
23+
}
24+
25+
protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(30);
26+
27+
protected override async Task<bool> ValidateAuthenticationStateAsync(
28+
AuthenticationState authenticationState, CancellationToken cancellationToken)
29+
{
30+
// Get the user manager from a new scope to ensure it fetches fresh data
31+
var scope = _scopeFactory.CreateScope();
32+
try
33+
{
34+
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<TUser>>();
35+
return await ValidateSecurityStampAsync(userManager, authenticationState.User);
36+
}
37+
finally
38+
{
39+
if (scope is IAsyncDisposable asyncDisposable)
40+
{
41+
await asyncDisposable.DisposeAsync();
42+
}
43+
else
44+
{
45+
scope.Dispose();
46+
}
47+
}
48+
}
49+
50+
private async Task<bool> ValidateSecurityStampAsync(UserManager<TUser> userManager, ClaimsPrincipal principal)
51+
{
52+
var user = await userManager.GetUserAsync(principal);
53+
if (user == null)
54+
{
55+
return false;
56+
}
57+
else if (!userManager.SupportsUserSecurityStamp)
58+
{
59+
return true;
60+
}
61+
else
62+
{
63+
var principalStamp = principal.FindFirstValue(_options.ClaimsIdentity.SecurityStampClaimType);
64+
var userStamp = await userManager.GetSecurityStampAsync(user);
65+
return principalStamp == userStamp;
66+
}
67+
}
68+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<UserSecretsId>aspnet-BlazorServerCaptureUser-e85a62ec-1a6b-484e-8e43-2e59fb19ac81</UserSecretsId>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.1" />
12+
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.1" />
13+
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="7.0.1" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.1" />
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.1" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace BlazorServerCaptureUser.Data;
5+
public class ApplicationDbContext : IdentityDbContext
6+
{
7+
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
8+
: base(options)
9+
{
10+
}
11+
}

0 commit comments

Comments
 (0)