【发布时间】:2017-03-13 12:26:19
【问题描述】:
我正在构建一个带有承载令牌身份验证的 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);
}
}
我搜索了这个错误,发现有人说它是通过以下方式解决的:
- Webconfig:设置
<modules runAllManagedModulesForAllRequests="true"> - 安装 Microsoft.Owin.Host.SystemWeb
- 检查我是否使用
Context.GetOwinContext()而不是Request.GetOwinContext()
Webconfig 选项不起作用。
我有 Host.SystemWeb 包。
而且我没有在任何地方调用 GetOwinContext。
有什么想法吗?
谢谢。
【问题讨论】:
标签: c# asp.net asp.net-web-api2 owin