Der innere Handler wurde nicht mit dem WebApi Delegating-Handler zugewiesen
Ich habe ein Problem damit, dass WebApi hier in meinem Code eine Ausnahme auslöst:
public class WebApiAuthenticationHandler : DelegatingHandler
{
private const string AuthToken = "AUTH-TOKEN";
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
var requestAuthTokenList = GetRequestAuthTokens(request);
if (ValidAuthorization(requestAuthTokenList))
{
// EXCEPTION is occuring here!....
return base.SendAsync(request, cancellationToken);
}
/*
** This will make the whole API protected by the API token.
** To only protect parts of the API then mark controllers/methods
** with the Authorize attribute and always return this:
**
** return base.SendAsync(request, cancellationToken);
*/
return Task<HttpResponseMessage>.Factory.StartNew(
() =>
{
var resp = new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
Content = new StringContent("Authorization failed")
};
//var resp = new HttpResponseMessage(HttpStatusCode.Unauthorized);
//resp.Headers.Add(SuppressFormsAuthenticationRedirectModule.SuppressFormsHeaderName,"true");
return resp;
});
}
Die Ausnahme passiert auf der Leitung:
base.SendAsync(request, cancellationToken);
Ich habe keine Ahnung, wie ich das beheben soll. In meiner Streckentabelle habe ich Folgendes:
routes.MapHttpRoute("NoAuthRequiredApi", "api/auth/", new { Controller = "Auth" });
routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional }, null, new WebApiAuthenticationHandler());
Die Route, auf der dies geschieht, ist die DefaultApi-Route. Jede Hilfe sehr geschätzt ....