Asp.Net WebApi 整合OWIN架構後,出現 “沒有 OWIN 身分識別驗證管理器與此請求相關聯” 的解決辦法

來源:互聯網
上載者:User

標籤:ack   handler   asa   win   challenge   eof   blog   trace   oci   

在Asp.Net WebApi 項目中開啟OWIN模組之後,如果沒有在OWIN的Startup類中配置認證方式,調用WebApi的相關Controller和Action就會出現如下異常:

出現錯誤。沒有 OWIN 身分識別驗證管理器與此請求相關聯。ExceptionType:System.InvalidOperationExceptionStackTrace:   在 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又預設啟用了身份認證,所以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類中指定預設的認證方式。

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

 

Asp.Net WebApi 整合OWIN架構後,出現 “沒有 OWIN 身分識別驗證管理器與此請求相關聯” 的解決辦法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.