Skip to content

Commit 86bf3b7

Browse files
committed
Updates
1 parent 673e3de commit 86bf3b7

File tree

1 file changed

+61
-11
lines changed

1 file changed

+61
-11
lines changed

aspnetcore/blazor/security/blazor-web-app-with-windows-authentication.md

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to secure a Blazor Web App with Windows Authentication.
55
monikerRange: '>= aspnetcore-9.0'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 02/12/2025
8+
ms.date: 02/13/2025
99
uid: blazor/security/blazor-web-app-windows-authentication
1010
---
1111
# Secure an ASP.NET Core Blazor Web App with Windows Authentication
@@ -16,11 +16,11 @@ uid: blazor/security/blazor-web-app-windows-authentication
1616
1717
-->
1818

19-
This article describes how to secure a Blazor Web App with [Windows Authentication]() using a sample app in the [`dotnet/blazor-samples` GitHub repository (.NET 9 or later)](https://github.com/dotnet/blazor-samples) ([how to download](xref:blazor/fundamentals/index#sample-apps)).
19+
This article describes how to secure a Blazor Web App with [Windows Authentication](/windows-server/security/windows-authentication/windows-authentication-overview) using a sample app in the [`dotnet/blazor-samples` GitHub repository (.NET 9 or later)](https://github.com/dotnet/blazor-samples) ([how to download](xref:blazor/fundamentals/index#sample-apps)).
2020

21-
Specification for the Blazor Web App:
21+
The app specification for the Blazor Web App:
2222

23-
* [Server render mode with global interactivity](xref:blazor/components/render-modes)
23+
* Adopts the [Interactive Server render mode with global interactivity](xref:blazor/components/render-modes).
2424
* Establishes an [authorization policy](xref:security/authorization/policies) for a [Windows security identifier](/windows-server/identity/ad-ds/manage/understand-security-identifiers) to access a secure page.
2525

2626
## Sample app
@@ -31,22 +31,22 @@ Access the sample app through the latest version folder from the repository's ro
3131

3232
## Configuration
3333

34-
This app requires no configuration to run locally.
34+
The sample app doesn't require configuration to run locally.
3535

3636
When deployed to a host, such as IIS, the app must adopt impersonation to run under the user's account. For more information, see <xref:security/authentication/windowsauth>.
3737

3838
### Sample app code
3939

40-
Inspect the `Program` file in the sample app for the following features.
40+
Inspect the `Program` file in the sample app for the following API calls.
4141

42-
<xref:Microsoft.Extensions.DependencyInjection.AuthenticationServiceCollectionExtensions.AddAuthentication%2A> is called using the <xref:Microsoft.AspNetCore.Authentication.Negotiate.NegotiateDefaults.AuthenticationScheme%2A?displayProperty=nameWithType> authentication scheme. <xref:Microsoft.Extensions.DependencyInjection.NegotiateExtensions.AddNegotiate%2A> configures the <xref:Microsoft.AspNetCore.Authentication.AuthenticationBuilder> to use Negotiate (also known as Windows, Kerberos, or NTLM) authentication. This authentication handler supports Kerberos on Windows and Linux servers:
42+
<xref:Microsoft.Extensions.DependencyInjection.AuthenticationServiceCollectionExtensions.AddAuthentication%2A> is called using the <xref:Microsoft.AspNetCore.Authentication.Negotiate.NegotiateDefaults.AuthenticationScheme%2A?displayProperty=nameWithType> authentication scheme. <xref:Microsoft.Extensions.DependencyInjection.NegotiateExtensions.AddNegotiate%2A> configures the <xref:Microsoft.AspNetCore.Authentication.AuthenticationBuilder> to use Negotiate (also known as Windows, Kerberos, or NTLM) authentication, and the authentication handler supports Kerberos on Windows and Linux servers:
4343

4444
```csharp
4545
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
4646
.AddNegotiate();
4747
```
4848

49-
<xref:Microsoft.Extensions.DependencyInjection.PolicyServiceCollectionExtensions.AddAuthorization%2A> adds authorization policy services, setting the <xref:Microsoft.AspNetCore.Authorization.AuthorizationOptions.FallbackPolicy%2A?displayProperty=nameWithType> to the default policy (<xref:Microsoft.AspNetCore.Authorization.AuthorizationOptions.DefaultPolicy%2A?displayProperty=nameWithType>), which defaults to require authenticated users to access the app.
49+
<xref:Microsoft.Extensions.DependencyInjection.PolicyServiceCollectionExtensions.AddAuthorization%2A> adds authorization policy services, setting the <xref:Microsoft.AspNetCore.Authorization.AuthorizationOptions.FallbackPolicy%2A?displayProperty=nameWithType> to the default policy (<xref:Microsoft.AspNetCore.Authorization.AuthorizationOptions.DefaultPolicy%2A?displayProperty=nameWithType>), which defaults to require authenticated users to access the app:
5050

5151
```csharp
5252
builder.Services.AddAuthorization(options =>
@@ -55,13 +55,13 @@ builder.Services.AddAuthorization(options =>
5555
});
5656
```
5757

58-
<xref:Microsoft.Extensions.DependencyInjection.CascadingAuthenticationStateServiceCollectionExtensions.AddCascadingAuthenticationState%2A> adds cascading authentication state to the service collection. This is equivalent to having a `CascadingAuthenticationState` component at the root of the app's component hierarchy:
58+
<xref:Microsoft.Extensions.DependencyInjection.CascadingAuthenticationStateServiceCollectionExtensions.AddCascadingAuthenticationState%2A> adds cascading authentication state to the service collection. This is equivalent to placing a `CascadingAuthenticationState` component at the root of the app's component hierarchy:
5959

6060
```csharp
6161
builder.Services.AddCascadingAuthenticationState();
6262
```
6363

64-
An [authorization policy](xref:security/authorization/policies) is added for a [Windows security identifier](/windows-server/identity/ad-ds/manage/understand-security-identifiers):
64+
An [authorization policy](xref:security/authorization/policies) is added for a [Windows security identifier (SID)](/windows-server/identity/ad-ds/manage/understand-security-identifiers). The `S-1-5-113` well-known SID in the following example indicates that the user is a local account, which restricts network sign-in to local accounts instead of "administrator" or equivalent accounts:
6565

6666
```csharp
6767
builder.Services.AddAuthorizationBuilder()
@@ -71,16 +71,66 @@ builder.Services.AddAuthorizationBuilder()
7171
"S-1-5-113"));
7272
```
7373

74-
The authorization policy is enforced by the `LocalAccountOnly` component (path: `/local-account-only`):
74+
The authorization policy is enforced by the `LocalAccountOnly` component.
75+
76+
`Components/Pages/LocalAccountOnly.razor`:
7577

7678
```razor
7779
@page "/local-account-only"
7880
@using Microsoft.AspNetCore.Authorization
7981
@attribute [Authorize("LocalAccount")]
82+
83+
<h1>Local Account Only</h1>
84+
85+
<p>
86+
You can only reach this page by satisfying the
87+
<code>LocalAccount</code> authorization policy.
88+
</p>
8089
```
8190

8291
The `UserClaims` component lists the user's claims, which includes the user's Windows security identifiers (SIDs).
8392

93+
`Components/Pages/UserClaims.razor`:
94+
95+
```razor
96+
@page "/user-claims"
97+
@using System.Security.Claims
98+
@using Microsoft.AspNetCore.Authorization
99+
@attribute [Authorize]
100+
101+
<PageTitle>User Claims</PageTitle>
102+
103+
<h1>User Claims</h1>
104+
105+
@if (claims.Any())
106+
{
107+
<ul>
108+
@foreach (var claim in claims)
109+
{
110+
<li><b>@claim.Type:</b> @claim.Value</li>
111+
}
112+
</ul>
113+
}
114+
115+
@code {
116+
private IEnumerable<Claim> claims = [];
117+
118+
[CascadingParameter]
119+
private Task<AuthenticationState>? AuthState { get; set; }
120+
121+
protected override async Task OnInitializedAsync()
122+
{
123+
if (AuthState == null)
124+
{
125+
return;
126+
}
127+
128+
var authState = await AuthState;
129+
claims = authState.User.Claims;
130+
}
131+
}
132+
```
133+
84134
## Additional resources
85135

86136
* <xref:security/authentication/windowsauth>

0 commit comments

Comments
 (0)