2017-06-09 6 views
1

OAuth2の認証と承認を実装しようとしました。私は認可サーバーとリソースサーバーを持っています。クライアントは許可サーバーにログインし(ユーザー名とパスワードを許可サーバーに送信します)、許可サーバーはaccess_tokenを戻します。クライアントは、resource_serverから[Authorize]タグを持つリソースを要求するために、access_tokenを使用します。OAuth2 401リソースサーバから無許可

認証パート(認証サーバーへの資格情報の送信とaccess_tokenの取得)が正常に機能します。私は有効なJWTトークンを取得します。 問題は、リソースサーバーがaccess_tokenを認識しないことです。クライアントが[Authorize]タグを持つリソースを取得する要求を送信するたびに、「この要求に対して401許可されていない承認が拒否されました」というメッセージが表示されます。

これは、私が検証/試したことのリストです:

  1. 私は、リソースおよび認証サーバ(バージョン2.1.0)の両方でまったく同じバージョンであることをMicrosoft.Owin.Security.OAuthをチェック
  2. リソースと認可サーバーの両方でclient_idとsecretが完全に同じバージョンであることを確認しました
  3. リソースと認可サーバーの両方に同じマシンキーがあることを確認しました(web.configファイルとiisで)
  4. 私はiisを確認しました
  5. 私はどこでもCORSを有効にしています
  6. 両方のサーバが同じマシンにあります。
  7. 私はこのようなヘッダリソースサーバに要求を検証し、トークンが認証に送信されます。Authorization:JWT eyJ0eXAiO.......JuRpuf6yWg
  8. 私の実装である私は郵便配達と同じ要求を送ったが、私は

同じ応答を取得これらの2つのチュートリアルに基づいて:

  1. http://bitoftech.net/2014/09/24/decouple-owin-authorization-server-resource-server-oauth-2-0-web-api/
  2. http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/

これは私のリソースサーバでStartup.csクラス:

using Microsoft.Owin.Cors; 
using Microsoft.Owin.Security; 
using Microsoft.Owin.Security.DataHandler.Encoder; 
using Microsoft.Owin.Security.Jwt; 
using Microsoft.Owin.Security.OAuth; 
using Owin; 
using System.Threading.Tasks; 
using System.Web.Http; 
using Web.Api.App_Start; 

namespace Web.Api 
{ 
    public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      HttpConfiguration config = new HttpConfiguration(); 
      ConfigureOAuth(app); 
      app.UseAutofacMiddleware((newAutofacContainer()) 
        .ConfigureContainer(config)); 
      app.UseCors(CorsOptions.AllowAll); 
      WebApiConfig.Register(config); 
      app.UseWebApi(config); 
     } 

     public void ConfigureOAuth(IAppBuilder app) 
     { 
      var issuer = "http://localhost:81/Auth.Server"; 
      var audience = "AUDIENCE"; 
      var secret = TextEncodings.Base64Url.Decode("SECRET"); 
      app.UseJwtBearerAuthentication(
       new JwtBearerAuthenticationOptions 
       { 
        AuthenticationMode = AuthenticationMode.Active, 
        AllowedAudiences = new[] { audience }, 
        IssuerSecurityTokenProviders = new 
          IIssuerSecurityTokenProvider[] 
          { 
          new SymmetricKeyIssuerSecurityTokenProvider(issuer, 
           secret) 
          }, 
        Provider = new OAuthBearerAuthenticationProvider 
        { 
         OnValidateIdentity = context => 
         { 
          context.Ticket.Identity.AddClaim(new 
           System.Security 
           .Claims.Claim("newCustomClaim", "newValue")); 
          return Task.FromResult<object>(null); 
         } 
        } 
       }); 

     } 
    } 
} 
+0

[SOLVED]:それがあるべき認証:**ベアラー** eyJ0eXAiO ....... JuRpuf6yWg(ベアラーではないJWT!) –

答えて

0

[SOLVED]:それはする必要がありますAuthorization:Bearer eyJ0eXAiO.......JuRpuf6yWgベアラませJWT!)

関連する問題