Asp.Net WebApi 使用OWIN架构后,出现 “没有 OWIN 身份验证管理器与此请求相关联(No OWIN authentication manager is associated wit...

 

在Asp.Net WebApi 项目中使用OWIN模块之后,如果没有在OWIN的Startup类中配置认证方式,调用WebApi的相关Controller和Action就会出现如下异常:


出现错误。
没有 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());
        //}

        app.UseCors(CorsOptions.AllowAll);
        app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
        {
          Authority = ConfigurationManager.AppSettings["authorcenter"],
          ValidationMode = ValidationMode.ValidationEndpoint,
          RequiredScopes = new[] { ConfigurationManager.AppSettings["scope"] }
        });


    } 

}

 

这样调用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即可。

 

原文链接

详细参考

转载于:https://www.cnblogs.com/sctacy/p/9644446.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值