kjenn31
kjenn31

Reputation: 77

.NetCore blazor authentication not working

I have set up the necessities to create an example to build off of. Here's my code.

StartUp File

    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>();
      

App.razor

<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>

customAuth

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

            var user = new ClaimsPrincipal(identity);

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



        }
    }
}

index

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

Given the code, the index page only shows "not Logged in". Am I missing something so simple that I am overlooking it? I am new to blazor.

Upvotes: 1

Views: 404

Answers (3)

Dmitry
Dmitry

Reputation: 654

You should call

NotifyAuthenticationStateChanged

When you use a Custom Provider, you should notify it when a user is Authenticated. Example: You may add this method to your Custom provider:

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)));
}

And please note, that you should create and call similar method when a user is logs out:

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)));
}

Upvotes: 0

Jason Chen
Jason Chen

Reputation: 166

You can use the default template. It has an example that works.

Upvotes: 0

Remi THOMAS
Remi THOMAS

Reputation: 920

You forgot to add CascadingAuthenticationState in 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>

Upvotes: 1

Related Questions