<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/from-api-key-to-user-with-asp-net-web-api-p3ugtmpf5" -->

---
title: From API key to user with ASP.NET Web API | daily.dev
description: A tutorial shows how to authenticate API consumers in ASP.NET Web API using a custom X-ApiKey HTTP header instead of forms, Windows, or basic authentication....
canonical: https://daily.dev/posts/from-api-key-to-user-with-asp-net-web-api-p3ugtmpf5
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: From API key to user with ASP.NET Web API | daily.dev
og:description: A tutorial shows how to authenticate API consumers in ASP.NET Web API using a custom X-ApiKey HTTP header instead of forms, Windows, or basic authentication....
og:url: https://daily.dev/posts/from-api-key-to-user-with-asp-net-web-api-p3ugtmpf5
og:image: https://api.daily.dev/og/posts/P3UgtmPF5.png
og:image:alt: From API key to user with ASP.NET Web API
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# From API key to user with ASP.NET Web API

**[Maarten Balliauw](https://daily.dev/sources/maartenballiauw)** · 4 min read · 0 upvotes · 0 comments

## Summary

A tutorial shows how to authenticate API consumers in ASP.NET Web API using a custom X-ApiKey HTTP header instead of forms, Windows, or basic authentication. It walks through building a DelegatingHandler called AuthorizationHeaderHandler that reads the header, maps it to a ClaimsIdentity and ClaimsPrincipal, and assigns it to the current thread so the standard [Authorize] attribute and User property work as expected. The approach is noted as adaptable to OAuth2 tokens as well.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://blog.maartenballiauw.be/posts/2012-10-18-from-api-key-to-user-with-aspnet-web-api>

## Questions this post answers

### How do I authenticate API requests using a custom API key header in ASP.NET Web API?

Create a DelegatingHandler that reads the custom header (for example X-ApiKey) from the incoming HttpRequestMessage, validates it, builds a ClaimsIdentity with the resolved username, wraps it in a ClaimsPrincipal, and assigns it to Thread.CurrentPrincipal. Register the handler with GlobalConfiguration.Configuration.MessageHandlers.Add so it runs on every request, letting [Authorize] and User.Identity.Name work normally.

_daily.dev surfaces practical ASP.NET Web API security patterns for developers wiring up custom authentication._

### Can a DelegatingHandler-based authentication approach in ASP.NET Web API be reused for OAuth2 tokens?

Yes, the same DelegatingHandler pattern used for a custom API key header can be adapted for OAuth2 tokens, since OAuth2 ultimately relies on a custom Authorization header at the HTTP level. A similar technique was applied for OAuth2 delegation with Windows Azure Access Control Service, handling OAuth2 tokens the same way the API key is translated into a ClaimsPrincipal.

_Developers choosing between API keys and OAuth2 can find implementation patterns like this on daily.dev._

## Similar posts on daily.dev

- [Api Key Authentication in ASP.NET Core](https://daily.dev/posts/api-key-authentication-in-asp-net-core-5jr6dwwxq) · We Are .NET · 3 upvotes · 0 comments
- [Role-Based Authorization in ASP.NET Core](https://daily.dev/posts/role-based-authorization-in-asp-net-core-qirbihaku) · C\# Corner · 7 upvotes · 0 comments
- [ASP.NET Core Security Explained: Modern Authentication, Authorization, and JWT](https://daily.dev/posts/asp-net-core-security-explained-modern-authentication-authorization-and-jwt-bgq4u3gn4) · Syncfusion · 4 upvotes · 0 comments
- [How to implement HMAC authentication in ASP.NET Core](https://daily.dev/posts/how-to-implement-hmac-authentication-in-asp-net-core-awauatewf) · InfoWorld · 1 upvotes · 0 comments
- [Claims-Based Authorization in ASP.NET Core](https://daily.dev/posts/claims-based-authorization-in-asp-net-core-mdwlbrcwo) · Code with Mukesh · 6 upvotes · 0 comments

---

Tags: [#authentication](https://daily.dev/tags/authentication), [#c#](https://daily.dev/tags/c#)

[View this post on daily.dev](https://daily.dev/posts/from-api-key-to-user-with-asp-net-web-api-p3ugtmpf5)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"From API key to user with ASP.NET Web API","url":"https://daily.dev/posts/from-api-key-to-user-with-asp-net-web-api-p3ugtmpf5","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/from-api-key-to-user-with-asp-net-web-api-p3ugtmpf5"},"datePublished":"2026-10-04T13:04:40.778Z","dateModified":"2026-10-04T13:08:07.751Z","description":"A tutorial shows how to authenticate API consumers in ASP.NET Web API using a custom X-ApiKey HTTP header instead of forms, Windows, or basic authentication....","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/085578f1fde5cd4b6400cf49287277c4?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/085578f1fde5cd4b6400cf49287277c4?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Maarten Balliauw","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Maarten Balliauw","logo":"https://media.daily.dev/image/upload/s--rOTg3VYO--/f_auto,q_auto/v1791119043/logos/maartenballiauw?_a=BAMAMicg0","url":"https://daily.dev/sources/maartenballiauw"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/from-api-key-to-user-with-asp-net-web-api-p3ugtmpf5","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"authentication,c#","timeRequired":"PT4M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Maarten Balliauw","item":"https://daily.dev/sources/maartenballiauw"},{"@type":"ListItem","position":3,"name":"From API key to user with ASP.NET Web API"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/from-api-key-to-user-with-asp-net-web-api-p3ugtmpf5#faq","mainEntity":[{"@type":"Question","name":"How do I authenticate API requests using a custom API key header in ASP.NET Web API?","acceptedAnswer":{"@type":"Answer","text":"Create a DelegatingHandler that reads the custom header (for example X-ApiKey) from the incoming HttpRequestMessage, validates it, builds a ClaimsIdentity with the resolved username, wraps it in a ClaimsPrincipal, and assigns it to Thread.CurrentPrincipal. Register the handler with GlobalConfiguration.Configuration.MessageHandlers.Add so it runs on every request, letting [Authorize] and User.Identity.Name work normally. daily.dev surfaces practical ASP.NET Web API security patterns for developers wiring up custom authentication."}},{"@type":"Question","name":"Can a DelegatingHandler-based authentication approach in ASP.NET Web API be reused for OAuth2 tokens?","acceptedAnswer":{"@type":"Answer","text":"Yes, the same DelegatingHandler pattern used for a custom API key header can be adapted for OAuth2 tokens, since OAuth2 ultimately relies on a custom Authorization header at the HTTP level. A similar technique was applied for OAuth2 delegation with Windows Azure Access Control Service, handling OAuth2 tokens the same way the API key is translated into a ClaimsPrincipal. Developers choosing between API keys and OAuth2 can find implementation patterns like this on daily.dev."}}]}
```

