Skip to main content

MCP Server Authentication in C# with Microsoft Entra ID

Verified Oct 2026 Advanced Original .NET 10 ModelContextProtocol 1.0.0 ModelContextProtocol.AspNetCore 1.0.0 Microsoft.Identity.Web 3.2.0
By Rajesh Mishra · Published: · 11 min read
In 30 Seconds

Securing remote MCP servers in C# requires authenticating HTTP transports with Microsoft Entra ID OAuth 2.0 bearer tokens. Using ModelContextProtocol.AspNetCore and Microsoft.Identity.Web, developers can protect MCP endpoints with ASP.NET Core JWT bearer middleware and enforce role- or scope-based authorization on specific tools. For automated AI agents, Azure.Identity DefaultAzureCredential automates client-side token acquisition.

What is MCP Server Authentication?

MCP server authentication is the security mechanism that verifies the identity of AI clients before allowing them to discover or execute tools over remote Model Context Protocol transports. While local MCP servers communicate securely over standard input/output (stdio) within a single host machine, production enterprise architectures host MCP servers centrally in ASP.NET Core over HTTP, requiring industry-standard OAuth 2.0 bearer tokens backed by Microsoft Entra ID.

Without transport-level authentication, any client with network access to your MCP server could invoke sensitive database queries, trigger internal APIs, or exfiltrate enterprise documents.

AI Agent / CopilotMicrosoft Entra IDASP.NET Core MCP ServerEnterprise Database 1. Authenticate (Client Credentials / PKCE)2. Issue JWT Bearer Token3. MCP Request + Authorization: Bearer <token>4. Validate Token & RequireScope('Mcp.Execute')5. Safe Tool Execution

1. Microsoft Entra ID Configuration

To protect your MCP server, register two applications in your Microsoft Entra ID tenant:

  1. MCP Server API App: Exposes application ID URI api://dotnetstudio-mcp-server and defines custom scopes:
    • Mcp.Tools.Read: Allows discovering available tools and schemas.
    • Mcp.Tools.Execute: Allows invoking tool functions.
  2. AI Client App: Represents the calling application (e.g., an automated agent background worker or a developer desktop tool). Granted permission to request api://dotnetstudio-mcp-server/Mcp.Tools.Execute.

In your appsettings.json, configure the AzureAd section:

{
  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "yourtenant.onmicrosoft.com",
    "TenantId": "72f988bf-86f1-41af-91ab-2d7cd011db47",
    "ClientId": "33333333-3333-3333-3333-333333333333",
    "Audience": "api://dotnetstudio-mcp-server"
  }
}

2. Server Implementation in ASP.NET Core (.NET 10)

Install the required NuGet packages into your ASP.NET Core project:

dotnet add package ModelContextProtocol.AspNetCore --version 1.0.0
dotnet add package Microsoft.Identity.Web --version 3.2.0
dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer --version 10.0.0

Configure Program.cs to enable JWT Bearer authentication, register MCP services, and enforce authorization policies on the MCP endpoint:

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Identity.Web;
using ModelContextProtocol.AspNetCore;
using ModelContextProtocol.Server;

var builder = WebApplication.CreateBuilder(args);

// 1. Add Microsoft Entra ID JWT Bearer validation
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));

// 2. Define authorization policies for MCP tool execution
builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("McpExecutePolicy", policy =>
    {
        policy.RequireAuthenticatedUser();
        policy.RequireScope("Mcp.Tools.Execute");
    });
});

// 3. Register MCP Server services
builder.Services.AddMcpServer(options =>
{
    options.ServerInfo = new Implementation
    {
        Name = "DotNetStudio Enterprise Tools",
        Version = "1.0.0"
    };
})
.AddTool<OrderManagementTools>();

var app = builder.Build();

app.UseAuthentication();
app.UseAuthorization();

// 4. Map the MCP endpoint with required authorization
app.MapMcpServer("/mcp")
   .RequireAuthorization("McpExecutePolicy");

app.Run();

3. Defining Protected MCP Tools

When registering tools in C#, you can validate caller identity directly from HttpContext or check custom claims:

using System.Security.Claims;
using ModelContextProtocol.Server;

public class OrderManagementTools
{
    private readonly IHttpContextAccessor _httpContextAccessor;
    private readonly IOrderRepository _orderRepo;

    public OrderManagementTools(
        IHttpContextAccessor httpContextAccessor,
        IOrderRepository orderRepo)
    {
        _httpContextAccessor = httpContextAccessor;
        _orderRepo = orderRepo;
    }

    [McpTool("get_customer_orders", "Retrieves recent orders for a verified customer account.")]
    public async Task<string> GetCustomerOrdersAsync(
        [McpParameter("Customer account ID", required: true)] string customerId,
        CancellationToken ct)
    {
        var user = _httpContextAccessor.HttpContext?.User;
        var callerId = user?.FindFirst(ClaimTypes.NameIdentifier)?.Value 
                      ?? user?.FindFirst("azp")?.Value;

        // Enterprise audit logging
        Console.WriteLine($"[AUDIT] Tool invoked by caller: {callerId} for customer: {customerId}");

        var orders = await _orderRepo.GetOrdersByCustomerAsync(customerId, ct);
        return System.Text.Json.JsonSerializer.Serialize(orders);
    }
}

4. Connecting from a C# Client with DefaultAzureCredential

When building an autonomous AI agent in C# using Semantic Kernel or Microsoft Agent Framework, authenticate outbound calls using Azure.Identity:

using Azure.Core;
using Azure.Identity;
using ModelContextProtocol.Client;
using ModelContextProtocol.Http;

var tokenCredential = new DefaultAzureCredential();
var tokenContext = new TokenRequestContext(new[] { "api://dotnetstudio-mcp-server/.default" });
var tokenResult = await tokenCredential.GetTokenAsync(tokenContext, CancellationToken.None);

// Configure MCP HTTP client with Bearer Token
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = 
    new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", tokenResult.Token);

var transport = new SseClientTransport(
    httpClient, 
    new Uri("https://mcp.internal.dotnetstudioai.com/mcp"));

var mcpClient = new McpClient(transport);
await mcpClient.ConnectAsync();

// Discover authorized tools
var tools = await mcpClient.ListToolsAsync();
foreach (var tool in tools)
{
    Console.WriteLine($"Discovered Authenticated Tool: {tool.Name}");
}

5. Security & Production Checklist

  1. Enforce HTTPS in Production: Never transmit bearer tokens over unencrypted HTTP. Ensure TLS 1.3 is enforced at your reverse proxy (Azure Front Door or AWS ALB).
  2. Short-Lived Tokens: Keep access token lifetimes between 15 and 60 minutes. MCP clients should implement token refresh handlers using DelegatingHandler.
  3. Audit Every Tool Execution: Log the calling application ID (appid or azp claim), IP address, and invoked tool parameters to an immutable audit store (Azure Monitor / OpenTelemetry).
  4. Least-Privilege Scopes: Separate read-only query tools from write/delete tools. Require elevated scopes (Mcp.Tools.Admin) for mutating actions.

⚠ Production Considerations

  • Do not log raw Authorization headers in MCP transport logging middleware — tokens can leak into application diagnostics or monitoring stores.
  • Clock skew between client machines and Microsoft Entra ID can reject tokens during high-volume tool call loops. Ensure TokenValidationParameters.ClockSkew is configured appropriately (default 5 minutes).

🧠 Architect’s Note

Securing MCP tools is zero-trust architecture for AI agents. Never expose database mutations or ERP execution tools on anonymous MCP endpoints. Treat an MCP server like an enterprise microservice: enforce identity validation, log audit trails with caller user IDs, and use fine-grained scopes.

AI-Friendly Summary

Summary

Securing remote MCP servers in C# requires authenticating HTTP transports with Microsoft Entra ID OAuth 2.0 bearer tokens. Using ModelContextProtocol.AspNetCore and Microsoft.Identity.Web, developers can protect MCP endpoints with ASP.NET Core JWT bearer middleware and enforce role- or scope-based authorization on specific tools. For automated AI agents, Azure.Identity DefaultAzureCredential automates client-side token acquisition.

Key Takeaways

  • MCP stdio transport operates on process boundaries; remote MCP servers require HTTP transport authentication
  • ASP.NET Core JWT Bearer authentication integrates directly with ModelContextProtocol.AspNetCore endpoints
  • Use Microsoft.Identity.Web 3.2+ with .NET 10 for streamlined Entra ID token validation
  • Tools can enforce granular scopes (e.g. Mcp.Tools.Execute vs Mcp.Tools.Admin) to restrict high-privilege operations
  • Azure.Identity DefaultAzureCredential enables seamless machine-to-machine authentication for enterprise AI agents

Implementation Checklist

  • Register MCP Server Web API application in Microsoft Entra ID admin center
  • Expose API scopes: Mcp.Tools.Execute and Mcp.Tools.Read
  • Install Microsoft.Identity.Web and ModelContextProtocol.AspNetCore NuGet packages
  • Configure AddMicrosoftIdentityWebApi in Program.cs with AzureAd config section
  • Apply RequireAuthorization() to MapMcpServer() route endpoint
  • Configure C# MCP client with DelegatingHandler or Authorization header

Frequently Asked Questions

How does authentication work in remote Model Context Protocol (MCP) servers?

Remote MCP servers hosted over HTTP (SSE or Streamable HTTP) rely on standard HTTP transport authentication. In .NET, incoming requests carry an Authorization: Bearer <token> header, validated by ASP.NET Core authentication middleware using Microsoft.Identity.Web before requests reach MCP tool handlers.

Can I use the [Authorize] attribute on individual MCP tools in C#?

Yes. With ModelContextProtocol.AspNetCore 1.0.0, tool registration methods and tool classes support ASP.NET Core endpoint metadata. You can apply [Authorize] or require specific OAuth scopes (e.g., Tools.Execute) per tool or across the entire MCP endpoint route.

What Microsoft Entra ID application type should I create for an MCP server?

Register an API Application (Web API) in Microsoft Entra ID. Expose custom scopes such as 'Mcp.Tools.Read' and 'Mcp.Tools.Execute'. Client applications (AI agents or desktops running Claude/Foundry) obtain access tokens via OAuth 2.0 Authorization Code or Client Credentials flow.

How do MCP clients running locally pass tokens to remote MCP servers?

In the MCP client configuration, headers can be statically or dynamically supplied. For production enterprise setups, the C# MCP client supports delegating handlers that retrieve tokens using Azure.Identity (DefaultAzureCredential) and attach Bearer tokens to outgoing HTTP requests.

Track your progress through this learning path.

You Might Also Enjoy

#MCP #Model Context Protocol #Microsoft Entra ID #Security #Authentication #ASP.NET Core #.NET 10

Was this article useful?

Feedback is anonymous and helps us improve content quality.