[英]WebApi2 - No OWIN authentication manager is associated with the request
我正在使用Bearer Token Authentication構建一個Web API 2項目。
對access_token的請求有效,但我的其他方法無效。 API返回以下內容:
沒有OWIN身份驗證管理器與此請求關聯
完整回應訊息
{
"Message":"An error has occurred.",
"ExceptionMessage":"No OWIN authentication manager is associated with the request.",
"ExceptionType":"System.InvalidOperationException",
"StackTrace":" at System.Web.Http.Owin.PassiveAuthenticationMessageHandler.SuppressDefaultAuthenticationChallenges(HttpRequestMessage request)\r\n at System.Web.Http.Owin.PassiveAuthenticationMessageHandler.<SendAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.HttpServer.<SendAsync>d__0.MoveNext()"
}
Startup.cs
public partial class Startup
{
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
static string PublicClientKey = "XXX";
public void ConfigureAuth(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientKey),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
app.UseOAuthBearerTokens(OAuthOptions);
}
}
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ControllerAndAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Global.asax中
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
我搜索了此錯誤,發現有人說可以通過以下方法解決該錯誤:
<modules runAllManagedModulesForAllRequests="true">
Context.GetOwinContext()
而不是Request.GetOwinContext()
Webconfig選項不起作用。
我有Host.SystemWeb包。
而且我沒有在任何地方調用GetOwinContext。
任何想法?
謝謝。
如異常所示,缺少身份驗證管理器。 為了解決這個問題,我將嘗試在Startup.cs類中重新配置承載令牌配置。
這樣嘗試
public void ConfigureAuth(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
//You don't need these lines if you are using bearer token as the token is
//passed in the request header and not in the cookie
//app.UseCookieAuthentication(new CookieAuthenticationOptions());
//app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientKey),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
//Remove this part
//app.UseOAuthBearerTokens(OAuthOptions);
//And try to manually define the authorization server
//and the middleware to handle the tokens
app.UseOAuthAuthorizationServer(OAuthOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
UPDATE
因此,問題似乎在於SupressDefaultHostAuthentication。 如果您不在主機中運行,則無需添加SupressDefaultHostAuthentication,因此請在WebApiConfig中將其刪除(有關更多信息,請參閱此答案的注釋)。 這是一篇有關該主題的不錯的博客文章,可以使課堂更好地進行。
當我發布使用MVC 5的Web API 2模板構建的網站並向實時IIS服務器提供“個人用戶”身份驗證時遇到了這個問題。 它在Visual Studio的IIS Express上本地運行良好,但是給了我No OWIN身份驗證管理器與完整IIS服務器上的請求錯誤相關聯 。
我的解決方案是在web.config中進行以下更改:
添加此密鑰(我從此答案中學到了):
<appSettings>
<add key="owin:AppStartup" value="[MyStartUpAssemblyNamespace].Startup, [MyAssemblyName]" />
...
</appSettings>
然后將<modules>
更改為<modules runAllManagedModulesForAllRequests="true">
,這是我從此答案中學到的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.