You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/blazor/security/server/index.md
-112Lines changed: 0 additions & 112 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -284,118 +284,6 @@ To store additional claims from external providers, see <xref:security/authentic
284
284
285
285
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>.
286
286
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.
if (user.Identityisnotnull&&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.
For more information, see the guidance on <xref:Microsoft.AspNetCore.Components.OwningComponentBase> in <xref:blazor/fundamentals/dependency-injection#owningcomponentbase>.
398
-
399
287
## Unauthorized content display while prerendering with a custom `AuthenticationStateProvider`
400
288
401
289
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