2017-03-01 83 views
2

我试图创建将在AWS托管一个新的Web应用程序。此应用程序需要通过OAuth2从我们的Azure Active Directory中对用户进行身份验证。这是我到目前为止工作,我曾经让我有步骤:使用OAuth2以对Azure的AD身份验证调用的WebAPI

1)我可以生成从“login.microsoftonline.com”在用户登录后一个“代码”。为此,我在Azure AD中建立了一个新的应用程序,我的Web应用程序指导用户登录。我还在Azure AD中设置了API应用程序,该应用程序用作查询字符串中的“resource”参数当我直接用户使用“代码”的login.microsoftonline.com端点

2)从上面#1产生的,我可以通过调用我的应用程序的/令牌端点生成我授权令牌。我可以通过传递相同的资源值(网址到我最终想使用的API),代码,我的客户端ID和我的客户端密钥来完成此操作。

3)来自#2的令牌响应上面发送的token_type,expires_in,scope,access_token,refresh_token和id_token属性都有值。我能够将id_token解码为JWT,并且它在索赔对象中显示登录用户的正确用户信息。

4)这里是我的失败然后我尝试打电话给正在使用我获得#3的上方传递该值在“授权”报头与该值是“承载XYZ123 ......”如果我不把任何授权的access_token也登记在AD天青我的API的应用程序在API应用程序中,我得到了我期望的结果,但是,如果我在该类或Get()方法上放置了[Authorize]属性,我总会得到401(未授权)。我确定还有其他东西需要连线,我只是不确定是什么。我发现这个资源:https://docs.microsoft.com/en-us/azure/api-management/api-management-howto-protect-backend-with-aad但会谈API管理注册我的API的,我不认为我需要做的(我绝对不希望,如果我没有)。

我创建将依赖于能够获得记录在其中,我假设我可以承载令牌提取用户的身份API,我只是不知道如何...

任何帮助和指导将不胜感激。

编辑包括工作解决方案: 下面是我在我的启动类中使用的每个接受的答案下面。请注意,“受众”值是我需要访问的API端点的URL。租户是我们在组织中绑定的自定义url,如果您没有提供有效的租户值,则可能会收到一个异常,其中显示“响应状态代码不表示成功:404(未找到)”。你需要在Azure Active Directory的组件,你可以从得到的NuGet:Install-Package Microsoft.Owin.Security.ActiveDirectory

public partial class Startup 
{ 
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864 
    public void ConfigureAuth(IAppBuilder app) 
    { 

     app.UseWindowsAzureActiveDirectoryBearerAuthentication(
      new WindowsAzureActiveDirectoryBearerAuthenticationOptions 
      { 
       Audience = "https://myapi.azurewebsites.net/", 
       Tenant = "my.custom.url.com", 
       TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters 
       { 
        ValidateIssuer = false 
       } 
      }); 
    } 
} 

这里是我使用,使从需要访问收到我的令牌应用程序的请求后的参数。

 body.Add(new KeyValuePair<string, string>("grant_type", "authorization_code")); 
     body.Add(new KeyValuePair<string, string>("code", code)); // code from response 
     body.Add(new KeyValuePair<string, string>("redirect_uri", "http://localhost:51015/redirect")); 
     body.Add(new KeyValuePair<string, string>("client_id", "xxxxxxx-8829-4294-b2c9-xxxxxxxxxx")); // client id of this application making the request 
     body.Add(new KeyValuePair<string, string>("client_secret", "PxxxxxxxxxxxxSnTJ4Uh63Voj+tkxxxxxxx=")); 
     body.Add(new KeyValuePair<string, string>("resource", "https://myapi.azurewebsites.net/")); // same value goes here that is in the audience value of the api, this is also the same value that is passed in the resource parameter of the query string on the redirect to the login to obtain the "code" in the previous step 

回答

3

这取决于你如何保护网络API。通常情况下,我们可以使用Azure的广告中使用了下面的代码保护的Web API:

public partial class Startup 
{ 
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864 
    public void ConfigureAuth(IAppBuilder app) 
    { 

     app.UseWindowsAzureActiveDirectoryBearerAuthentication(
      new WindowsAzureActiveDirectoryBearerAuthenticationOptions 
      { 
       Audience = ConfigurationManager.AppSettings["ida:Audience"], 
       Tenant = ConfigurationManager.AppSettings["ida:Tenant"], 
       TokenValidationParameters= new System.IdentityModel.Tokens.TokenValidationParameters { 
        ValidateIssuer=false 
       } 
      }); 
    } 
} 

观众是应用程序的客户端Id你在Azure门户注册。在这种情况下,我们使用这个应用程序作为客户端和资源。然后,我们canrequest访问令牌像下面的要求:

POST: https://login.microsoftonline.com/{tenantId}/oauth2/token 
resource={clientId}&client_id={clientId}&code={authorizationCode}&grant_type=authorization_code&redirect_uri={redirectUri}&client_secret={clientSecret} 

此标记应为网络API作为通过上面的代码保护的授权作品。有关保护Web API的更多细节,请参阅here

+1

请注意POST请求中的两个客户端ID不一样。 'resource'是API的客户端ID,'client_id'是调用应用程序的客户端ID。另外,我发现您可以使用客户端ID或API的资源URI来请求令牌。尽管令牌中的观众将是客户端ID。 – juunas

+0

我想我已经尝试了受众部分和查询字符串中的客户端ID,资源(URI)等的每个组合,以检索该令牌并仍然获得401s。我不确定哪个应用程序的客户端ID等位置在哪里(甚至使用@juunas注释作为指南。你能说出究竟需要去哪里吗?我还应该使用GUID作为客户端ID还是应该使用一个URI –

+1

没关系,我已经改变了我作为一个测试通过的令牌,但我现在已经开始工作了。非常感谢你们两位的帮助!+ 1对你们来说:) –

0

入住提琴手任何其他详细信息”相关的认证失败,有可能是像信息‘无效的观众’,等你需要知道失败(401错误)来解决这个问题的根本原因。

相关问题