Asp.net

我收到“此請求的授權已被拒絕”。使用 OWIN oAuth 中間件時出現錯誤消息(使用單獨的身份驗證和資源伺服器)

  • October 20, 2014

我正在嘗試解耦我的身份驗證和資源伺服器。我正在遵循本教程中提供的範例:

<http://bitoftech.net/2014/09/24/decouple-owin-authorization-server-resource-server-oauth-2-0-web-api/>

這是我的身份驗證伺服器中的 Startup.cs 中的程式碼:

using Microsoft.Owin;
using Microsoft.Owin.Security.OAuth;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AuthServer.Web
{
  public class xxxxx
  {
     public void Configuration(IAppBuilder app) {
        ConfigureOAuth(app);
     }

     public void ConfigureOAuth(IAppBuilder app)
     {
        OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
        {
           AllowInsecureHttp = true,
           TokenEndpointPath = new PathString("/token"),
           AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
           Provider = new SimpleAuthorizationServerProvider()
        };

        // Token Generation
        app.UseOAuthAuthorizationServer(OAuthServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

     }
  }
}

這是我的資源伺服器中的 startup.cs(即我的範例 Web api 應用程序):

using Microsoft.Owin.Security.OAuth;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AuthTestApi.Web
{
  public class Startup
  {
     public void Configuration(IAppBuilder app)
     {
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
     }      
  }
}

當我將以下請求發佈到我的身份驗證伺服器的“\token”端點時,我成功收到了一個令牌:

{
access_token: "PszrzJUtQUhX42GMryWjZiVHuKiJ9yCVH_1tZURumtC5mTj2tpuRDF3tXcN_VNIYuXY40IG0K7W3KASfJ9DlNIU2jMOkL2U5oEAXLNRRQuNYdEJ7dMPb14nW19JIaM4BMk00xfQ8MFRw0p6-uoh0-e-Q6iAiTwDNN3F7bYMF9qm874qhLWEcOt6dWQgwpdDUVPDi7F07-Ck0zAs48Dg5w4q93vDpFaQMrziJg9aaxN8",
token_type: "bearer",
expires_in: 86399
}

當我向我的控制器發布以下請求時,我收到“此請求的授權已被拒絕”錯誤消息?

GET /api/Test HTTP/1.1
Host: localhost:63305
Accept: application/json
Content-Type: application/json
Authorization: Bearer PszrzJUtQUhX42GMryWjZiVHuKiJ9yCVH_1tZURumtC5mTj2tpuRDF3tXcN_VNIYuXY40IG0K7W3KASfJ9DlNIU2jMOkL2U5oEAXLNRRQuNYdEJ7dMPb14nW19JIaM4BMk00xfQ8MFRw0p6-uoh0-e-Q6iAiTwDNN3F7bYMF9qm874qhLWEcOt6dWQgwpdDUVPDi7F07-Ck0zAs48Dg5w4q93vDpFaQMrziJg9aaxN8
Cache-Control: no-cache
Postman-Token: aeca8515-70b1-ef2c-f317-bf66136dccab

我的身份驗證伺服器和資源/Web api 項目在不同的解決方案中,並且在不同的埠上執行(……不確定這是否重要,但我認為我提到了它)。

此時,這兩個項目正在使用 oAuth OWIN 中間件(並且幾乎沒有自定義程式碼)。中間件有點黑盒,只需要一些幫助來弄清楚我為什麼會收到此錯誤消息。

另請注意,我在兩個 Visual Studio 2013 Web 應用程序項目中執行兩台伺服器,這些項目位於不同埠上執行的不同 VS 2013 解決方案中。我不確定這是否重要,但我想我會提到它。

提前致謝。

在過去的幾天裡,我遇到了完全相同的問題,儘管我將兩個應用程序都託管在同一台機器上(不同的埠),但是如果不添加兩個 web.config 文件的機器密鑰確保相同的軟體包版本,它就無法工作兩個應用程序中安裝的編號

我剛剛遇到了同樣的問題並找到了解決方案:

您需要在註冊WebAPI之前註冊 OAuth Token Generator 和 OAuth Token Consumer 事物。

如果您將其視為一個管道,則有點道理,其中身份驗證/授權應該在控制器處理任何請求之前進行。

TL;DR:改變

appBuilder.UseWebApi(config);

this.ConfigureOAuthTokenGenerator(appBuilder);
this.ConfigureOAuthConsumer(appBuilder);

this.ConfigureOAuthTokenGenerator(appBuilder);
this.ConfigureOAuthConsumer(appBuilder);

appBuilder.UseWebApi(config);

引用自:https://stackoverflow.com/questions/26458785