Skip to content

Commit 7c17a3b

Browse files
authored
[Blazor] Server security - Inject AuthenticationStateProvider for services scoped to a component - section removal
1 parent 73d14c8 commit 7c17a3b

File tree

1 file changed

+0
-112
lines changed
  • aspnetcore/blazor/security/server

1 file changed

+0
-112
lines changed

aspnetcore/blazor/security/server/index.md

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -284,118 +284,6 @@ To store additional claims from external providers, see <xref:security/authentic
284284

285285
Specify the issuer explicitly when deploying to Azure App Service on Linux with Identity Server. For more information, see <xref:security/authentication/identity/spa#azure-app-service-on-linux>.
286286

287-
## Inject `AuthenticationStateProvider` for services scoped to a component
288-
289-
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.
290-
291-
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.
292-
293-
`ExampleService.cs`:
294-
295-
```csharp
296-
public class ExampleService
297-
{
298-
public async Task<string> ExampleMethod(AuthenticationStateProvider authStateProvider)
299-
{
300-
var authState = await authStateProvider.GetAuthenticationStateAsync();
301-
var user = authState.User;
302-
303-
if (user.Identity is not null && user.Identity.IsAuthenticated)
304-
{
305-
return $"{user.Identity.Name} is authenticated.";
306-
}
307-
else
308-
{
309-
return "The user is NOT authenticated.";
310-
}
311-
}
312-
}
313-
```
314-
315-
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).
316-
317-
:::moniker range=">= aspnetcore-6.0"
318-
319-
In the `Program` file:
320-
321-
```csharp
322-
builder.Services.AddScoped<ExampleService>();
323-
```
324-
325-
:::moniker-end
326-
327-
:::moniker range="< aspnetcore-6.0"
328-
329-
In `Startup.ConfigureServices` of `Startup.cs`:
330-
331-
```csharp
332-
services.AddScoped<ExampleService>();
333-
```
334-
335-
:::moniker-end
336-
337-
In the following `InjectAuthStateProvider` component:
338-
339-
* The component inherits <xref:Microsoft.AspNetCore.Components.OwningComponentBase>.
340-
* The <xref:Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider> is injected and passed to `ExampleService.ExampleMethod`.
341-
* `ExampleService` is resolved with <xref:Microsoft.AspNetCore.Components.OwningComponentBase.ScopedServices?displayProperty=nameWithType> and <xref:Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService%2A>, which returns the correct, initialized instance of `ExampleService` that exists for the lifetime of the user's circuit.
342-
343-
`InjectAuthStateProvider.razor`:
344-
345-
:::moniker range=">= aspnetcore-8.0"
346-
347-
```razor
348-
@page "/inject-auth-state-provider"
349-
@inherits OwningComponentBase
350-
@inject AuthenticationStateProvider AuthenticationStateProvider
351-
352-
<h1>Inject <code>AuthenticationStateProvider</code> Example</h1>
353-
354-
<p>@message</p>
355-
356-
@code {
357-
private string? message;
358-
private ExampleService? ExampleService { get; set; }
359-
360-
protected override async Task OnInitializedAsync()
361-
{
362-
ExampleService = ScopedServices.GetRequiredService<ExampleService>();
363-
364-
message = await ExampleService.ExampleMethod(AuthenticationStateProvider);
365-
}
366-
}
367-
```
368-
369-
:::moniker-end
370-
371-
:::moniker range="< aspnetcore-8.0"
372-
373-
```razor
374-
@page "/inject-auth-state-provider"
375-
@inject AuthenticationStateProvider AuthenticationStateProvider
376-
@inherits OwningComponentBase
377-
378-
<h1>Inject <code>AuthenticationStateProvider</code> Example</h1>
379-
380-
<p>@message</p>
381-
382-
@code {
383-
private string? message;
384-
private ExampleService? ExampleService { get; set; }
385-
386-
protected override async Task OnInitializedAsync()
387-
{
388-
ExampleService = ScopedServices.GetRequiredService<ExampleService>();
389-
390-
message = await ExampleService.ExampleMethod(AuthenticationStateProvider);
391-
}
392-
}
393-
```
394-
395-
:::moniker-end
396-
397-
For more information, see the guidance on <xref:Microsoft.AspNetCore.Components.OwningComponentBase> in <xref:blazor/fundamentals/dependency-injection#owningcomponentbase>.
398-
399287
## Unauthorized content display while prerendering with a custom `AuthenticationStateProvider`
400288

401289
To avoid showing unauthorized content, for example content in an [`AuthorizeView` component](xref:blazor/security/index#authorizeview-component), while prerendering with a [custom `AuthenticationStateProvider`](xref:blazor/security/authentication-state#implement-a-custom-authenticationstateprovider), adopt ***one*** of the following approaches:

0 commit comments

Comments
 (0)