Asp.Net WebApi 使用OWIN架构后,出现 “没有 OWIN 身份验证管理器与此请求相关联” 异常的解决办法
2017-05-24 19:57
796 查看
在Asp.Net WebApi 项目中使用OWIN模块之后,如果没有在OWIN的Startup类中配置认证方式,调用WebApi的相关Controller和Action就会出现如下异常:
如果是英文版的VisualStudio,以上异常信息会是:No OWIN authentication manager is associated with the request
原因是因为我们在Asp.Net WebApi项目使用了OWIN框架,但是没有指定OWIN框架使用的认证方式,而WebApi也禁用了默认的身份认证,所以到来的Http请求无法进行任何身份认证(意思就是匿名访问都不允许),抛出了异常。
我们可以看到下面的OWIN框架Startup类的Configuration方法为空,没有为OWIN指定身份认证方式。
这样调用WebApi的相关Controller和Action就不会再抛出异常了。
第二:在Asp.Net WebApi的全局配置文件WebApiConfig.cs中取消调用SuppressDefaultHostAuthentication方法,来启用Host的默认身份认证,如下代码所示。当然如果项目中本来就没有代码调用SuppressDefaultHostAuthentication方法,就不用考虑这个解决方法了。
当然如果你的ASP.NET项目中没有安装或者丢失了NuGet包:Microsoft.Owin.Host.SystemWeb,但是又使用了OWIN框架,也会出现本文所述的错误,只要重新安装NuGet包:Microsoft.Owin.Host.SystemWeb即可。
详情还可以查看这个链接的帖子内容
出现错误。 没有 OWIN 身份验证管理器与此请求相关联。 ExceptionType:System.InvalidOperationException StackTrace: 在 System.Web.Http.Owin.PassiveAuthenticationMessageHandler.SuppressDefaultAuthenticationChallenges(HttpRequestMessage request) 在 System.Web.Http.Owin.PassiveAuthenticationMessageHandler.<SendAsync>d__0.MoveNext() --- 引发异常的上一位置中堆栈跟踪的末尾 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 System.Web.Http.HttpServer.<SendAsync>d__0.MoveNext()
如果是英文版的VisualStudio,以上异常信息会是:No OWIN authentication manager is associated with the request
原因是因为我们在Asp.Net WebApi项目使用了OWIN框架,但是没有指定OWIN框架使用的认证方式,而WebApi也禁用了默认的身份认证,所以到来的Http请求无法进行任何身份认证(意思就是匿名访问都不允许),抛出了异常。
我们可以看到下面的OWIN框架Startup类的Configuration方法为空,没有为OWIN指定身份认证方式。
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Owin; using Microsoft.Owin.Security; using Microsoft.Owin.Security.Cookies; using Owin; [assembly: OwinStartup(typeof(Daimler.CdnMgmt.Web.Startup))] namespace Daimler.CdnMgmt.Web { public partial class Startup { public void Configuration(IAppBuilder app) { } } }
解决方法有两个:
第一:在OWIN的Startup类中指定默认的认证方式,我们将上面的Startup类代码改为下面的代码,在Configuration方法中为指定认证方式为Cookie认证。using System; using System.Collections.Generic; using System.Linq; using Microsoft.Owin; using Microsoft.Owin.Security; using Microsoft.Owin.Security.Cookies; using Owin; [assembly: OwinStartup(typeof(Daimler.CdnMgmt.Web.Startup))] namespace Daimler.CdnMgmt.Web { public partial class Startup { public void Configuration(IAppBuilder app) { app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); app.UseCookieAuthentication(new CookieAuthenticationOptions()); } } }
这样调用WebApi的相关Controller和Action就不会再抛出异常了。
第二:在Asp.Net WebApi的全局配置文件WebApiConfig.cs中取消调用SuppressDefaultHostAuthentication方法,来启用Host的默认身份认证,如下代码所示。当然如果项目中本来就没有代码调用SuppressDefaultHostAuthentication方法,就不用考虑这个解决方法了。
using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Web.Http; using System.Web.Http.Cors; using Daimler.CdnMgmt.Web.Utils; using Microsoft.Owin.Security.OAuth; using Newtonsoft.Json.Serialization; namespace Daimler.CdnMgmt.Web { public static class WebApiConfig { public static void Register(HttpConfiguration config) { //config.SuppressDefaultHostAuthentication();//取消调用SuppressDefaultHostAuthentication方法,恢复Host的默认身份认证 // Web API 路由 config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new {id = RouteParameter.Optional} ); } } }
当然如果你的ASP.NET项目中没有安装或者丢失了NuGet包:Microsoft.Owin.Host.SystemWeb,但是又使用了OWIN框架,也会出现本文所述的错误,只要重新安装NuGet包:Microsoft.Owin.Host.SystemWeb即可。
详情还可以查看这个链接的帖子内容
相关文章推荐
- asp.net中ListView控件动态绑定数据源后,使用DataPager出现错误的原因及解决办法
- VS2012使用验证控件出现[ASP.NET]WebForms UnobtrusiveValidationMode 需要 'jquery' 的 ScriptResourceMapping。請加入 ScriptResourceMapping 命名的 jquery (區分大小寫)。的解决办法。
- 使用Asp.net MVC 2.0 +.NET 4.0 出现 “从客户端 ... 中检测到有潜在危险的 Request.Form 值”错误的解决办法
- 在使用ASP.NET网站管理工具出现“遇到错误。请返回上一页并重试。”的解决办法!
- asp.net中ListView控件动态绑定数据源后,使用DataPager出现错误的原因及解决办法
- wp7使用POST请求Asp.net网页时,HttpWebRequest的BeginGetResponse方法抛出NotSupportException异常的解决方法
- 如何解决asp.net中使用webbrowser是出现的"无法实例化ActiveX 控件"的异常"
- 在使用ASP.NET网站管理工具出现“遇到错误。请返回上一页并重试。”的解决办法!
- 终于找到了在asp.net使用popcalendar.js 日历时候提示出现"缺少对象“的解决办法。
- asp.net 发送 xml post请求和接收 xml post请求时出现(500) 内部服务器错误的解决办法
- 访问asp页面出现出现“请求的资源在使用中”的解决办法
- asp.net 使用Ueditor过程中出现的问题及解决办法
- IIS与ASP.Net没有关联解决办法
- 使用Asp.net Ajax时出现'Sys'未定义错误的解决办法
- (iis,asp.net)Tip:错误"由于 ASP.NET 进程标识对全局程序集缓存没有读权限,因此未能执行请求。错误: 0x80070005 拒绝访问"的解决办法
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- IIS中网站属性里没有ASP.NET选项的解决办法
- 使用C#操作OFFICE文件时出现异常的解决办法
- 企业库使用异常不允许所请求的注册表访问权 解决办法
- Asp.net页面使用showModalDialog时Postback弹出新页面解决办法