1
votes

L'authentification blazor .Net Core ne fonctionne pas

J'ai mis en place les éléments nécessaires pour créer un exemple sur lequel bâtir. Voici mon code.

Fichier de démarrage

<AuthorizeView>
    <Authorized>
        <p>Welcome, @context.User.Identity.Name</p>
    </Authorized>
    <NotAuthorized>
        <p>Not Logged In</p>
    </NotAuthorized>
</AuthorizeView>

App.razor

    public class CustomAuthenticationStateProvider : AuthenticationStateProvider
    {
        public override Task<AuthenticationState> GetAuthenticationStateAsync()
        {
            var identity = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.Name, "john.smith@gmail.com"),
            },   "apiauth_type");

            var user = new ClaimsPrincipal(identity);

            return Task.FromResult(new AuthenticationState(user));



        }
    }
}

customAuth

<Router AppAssembly="@typeof(Program).Assembly">
    <Found Context="routeData">
        <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
    </Found>
    <NotFound>
        <CascadingAuthenticationState>
            <LayoutView Layout="@typeof(MainLayout)">
                <p>Sorry, there's nothing at this address.</p>
            </LayoutView>
        </CascadingAuthenticationState>
    </NotFound>
</Router>

index

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();
        services.AddServerSideBlazor();
        services.AddSingleton<WeatherForecastService>();
        services.AddSingleton<DoggoDataServices>();
        services.AddSingleton<AddDoggoServices>();
        services.AddSingleton<EventServices>();
        services.AddScoped<AuthenticationStateProvider, CustomAuthenticationStateProvider>();
      

Compte tenu du code, la page d'index affiche uniquement "non connecté". Est-ce que je manque quelque chose de si simple que je l'oublie Je suis nouveau dans Blazor.


0 commentaires

3 Réponses :


1
votes

Vous avez oublié d'ajouter CascadingAuthenticationState dans app.razor

<CascadingAuthenticationState>
    <UserSession>
        <Router AppAssembly="@typeof(Program).Assembly">
            <Found Context="routeData">
                <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
            </Found>
            <NotFound>
                <LayoutView Layout="@typeof(MainLayout)">
                    <p>Sorry, there's nothing at this address.</p>
                </LayoutView>
            </NotFound>
        </Router>
    </UserSession>
</CascadingAuthenticationState>


1 commentaires

Merci pour votre perspicacité. J'ai inclus CascadingAuthenticationState dans mon code ci-dessus, sous la balise introuvable; mais j'ai tenté ce que vous avez mentionné par curiosité et toujours pas de chance.



0
votes

Vous pouvez utiliser le modèle par défaut. Il a un exemple qui fonctionne.


0 commentaires

0
votes

Vous devez appeler

public void LogoutUser()
{
            // reset identities and other related info (localstorage data if you have, etc).
            var identity = new ClaimsIdentity();
            var user = new ClaimsPrincipal(identity);
            NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(user)));
}

Lorsque vous utilisez un fournisseur personnalisé, vous devez le notifier lorsqu'un utilisateur est authentifié. Exemple: Vous pouvez ajouter cette méthode à votre fournisseur personnalisé:

public void MarkUserAsAuthenticated(Users u)
{
   // add your claims here. This is just an example.
    var identity = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.Name, u.UserName)
             });

    var user = new ClaimsPrincipal(identity);
     NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(user)));
}

Et veuillez noter que vous devez créer et appeler une méthode similaire lorsqu'un utilisateur se déconnecte:

NotifyAuthenticationStateChanged


0 commentaires