如何使用WebApi验证IdentityServer3颁发的令牌

时间:2016-11-09 12:29:37

标签: rest api authorization asp.net-identity identityserver3

我需要帮助来了解以下情况中发生的情况。

我的架构如下所示。

0 - 演示 1 - 身份验证服务器(使用IdentityServer3) 2 - 使用OData的隔离WebApi(没有AspNet.Identity的默认控制器) 3 - 商业服务 4 - 自定义存储库 5 - 存储库通用和数据访问层

数字0和2的图层必须通过第1层执行身份验证。

使用隐式流程验证身份验证对我的1号表示层非常有效。

但是,WebApi第2小服务层无法通过传递身份验证服务器提供的令牌进行身份验证。 我收到了以下消息。

{
  "odata.error": {
    "code": "",
    "message": {
      "lang": "en-US",
      "value": "Authorization has been denied for this request."
    }
  }
}

WebApi中的我的ConfigAuth

public static void ConfigureAuth(IAppBuilder app)
        {
            JwtSecurityTokenHandler.InboundClaimTypeMap.Clear();

            app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
            {
                ClientId = "WebApi",
                ClientSecret = "secret",                
                Authority = "http://localhost:35373/identity",      // TODO: Vai mudar com a versão final
                RequiredScopes = new[] { "read" , "write" , "offline_access" }

            });

            app.UseWebApi(WebApiConfig.Register());            
            app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);            
            app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
        }

这是我来自identityserver3的请求令牌

{
  "access_token": "5f4ca4bc0514158409903080af5c1081",
  "expires_in": 3600,
  "token_type": "Bearer",
  "refresh_token": "a8cd624dd3169a607bdb24ad0833d1b3"
}

0 个答案:

没有答案