Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 776|回复: 0

Asp.Net WebApi 使用OWIN架构后,出现 “没有 OWIN 身份验证管理器与此请求相关联” 异常的解决办法

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-4-25 09:11:27 | 显示全部楼层 |阅读模式

    在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());
            }
        }
    }

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

     

    详情还可以查看这个链接的帖子内容

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-7 16:51 , Processed in 0.065005 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表