Skip to content

Commit 27ed27b

Browse files
authored
Improve article section
1 parent c830a92 commit 27ed27b

File tree

1 file changed

+4
-50
lines changed
  • aspnetcore/blazor/security/server

1 file changed

+4
-50
lines changed

aspnetcore/blazor/security/server/index.md

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ Specify the issuer explicitly when deploying to Azure App Service on Linux with
282282

283283
## Inject `AuthenticationStateProvider` for services scoped to a component
284284

285-
Don't attempt to resolve <xref:Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider> within a custom scope because it results in the creation of a new instance of the <xref:Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider> that isn't correctly initialized.
285+
Don't attempt to resolve <xref:Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider> within a service that's scoped to a component by [`OwningComponentBase`](xref:fundamentals/dependency-injection#owningcomponentbase) because it results in the creation of a new instance of the <xref:Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider> that isn't correctly initialized.
286286

287-
To access the <xref:Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider> within a service scoped to a component, inject the <xref:Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider> with the [`@inject` directive](xref:mvc/views/razor#inject) or the [`[Inject]` attribute](xref:Microsoft.AspNetCore.Components.InjectAttribute) and pass it to the service as a parameter. This approach ensures that the correct, initialized instance of the <xref:Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider> is used for each user app instance.
287+
To access the <xref:Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider> within a service scoped to a component, inject the <xref:Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider> with the [`@inject` directive](xref:mvc/views/razor#inject) or the [`[Inject]` attribute](xref:Microsoft.AspNetCore.Components.InjectAttribute) into the component and pass the service instance to the scoped service as a parameter. This approach ensures that the correct, initialized instance of the <xref:Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider> is used for each user app instance.
288288

289289
`ExampleService.cs`:
290290

@@ -308,28 +308,12 @@ public class ExampleService
308308
}
309309
```
310310

311-
Register the service as scoped. In a server-side Blazor app, scoped services have a lifetime equal to the duration of the client connection [circuit](xref:blazor/hosting-models#blazor-server).
312-
313-
:::moniker range=">= aspnetcore-6.0"
314-
315-
In the `Program` file:
311+
In the `Program` file, the `ExampleService` is registered as a scoped service. In a server-side Blazor app, scoped services have a lifetime equal to the duration of the client connection [circuit](xref:blazor/hosting-models#blazor-server).
316312

317313
```csharp
318-
builder.Services.AddScoped<ExampleService>();
314+
.AddScoped<ExampleService>();
319315
```
320316

321-
:::moniker-end
322-
323-
:::moniker range="< aspnetcore-6.0"
324-
325-
In `Startup.ConfigureServices` of `Startup.cs`:
326-
327-
```csharp
328-
services.AddScoped<ExampleService>();
329-
```
330-
331-
:::moniker-end
332-
333317
In the following `InjectAuthStateProvider` component:
334318

335319
* The component inherits <xref:Microsoft.AspNetCore.Components.OwningComponentBase>.
@@ -338,8 +322,6 @@ In the following `InjectAuthStateProvider` component:
338322

339323
`InjectAuthStateProvider.razor`:
340324

341-
:::moniker range=">= aspnetcore-8.0"
342-
343325
```razor
344326
@page "/inject-auth-state-provider"
345327
@inherits OwningComponentBase
@@ -362,34 +344,6 @@ In the following `InjectAuthStateProvider` component:
362344
}
363345
```
364346

365-
:::moniker-end
366-
367-
:::moniker range="< aspnetcore-8.0"
368-
369-
```razor
370-
@page "/inject-auth-state-provider"
371-
@inject AuthenticationStateProvider AuthenticationStateProvider
372-
@inherits OwningComponentBase
373-
374-
<h1>Inject <code>AuthenticationStateProvider</code> Example</h1>
375-
376-
<p>@message</p>
377-
378-
@code {
379-
private string? message;
380-
private ExampleService? ExampleService { get; set; }
381-
382-
protected override async Task OnInitializedAsync()
383-
{
384-
ExampleService = ScopedServices.GetRequiredService<ExampleService>();
385-
386-
message = await ExampleService.ExampleMethod(AuthenticationStateProvider);
387-
}
388-
}
389-
```
390-
391-
:::moniker-end
392-
393347
For more information, see the guidance on <xref:Microsoft.AspNetCore.Components.OwningComponentBase> in <xref:blazor/fundamentals/dependency-injection#owningcomponentbase>.
394348

395349
## Unauthorized content display while prerendering with a custom `AuthenticationStateProvider`

0 commit comments

Comments
 (0)