Skip to content

Enable antiforgery token validation for ASP.NET Identity API endpoints #31181

Open

Description

Description

The following doesn't seem to work:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAntiforgery(opt =>
        {
            ...
        });

        services.AddIdentityApiEndpoints<User>(opt => opt.SignIn.RequireConfirmedAccount = true)
            .AddRoles<Role>()
            .AddEntityFrameworkStores<AppDb>();
        services.Configure<IdentityOptions>();
        ...
    }

    public void Configure(IApplicationBuilder app)
    {
        ...
        app.UseAuthentication();
        app.UseAuthorization();
        app.UseAntiforgery();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapGroup("/api/user")
            .MapIdentityApi<User>()
            .WithMetadata(new RequireAntiforgeryTokenAttribute());
            endpoints.MapControllers().WithMetadata(new RequireAntiforgeryTokenAttribute());
        });
    }

For other endpoints it works as expected (throws an error when tokens are missing), but Identity API endpoints are not validating antiforgery tokens.

Page URL

https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-8.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/security/authentication/identity-api-authorization.md

Document ID

137d4b94-7b26-3911-b22d-42c754a95fc1

Article author

@JeremyLikness

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions