c# - Azure Active Directory WebApi - 服务器到服务器

标签 c# azure asp.net-web-api azure-active-directory

我正在尝试使用 azure Active Directory 验证多个客户端(从 Windows 服务器应用程序连接 - 无浏览器)对我的 .Net4.51 webAPI 的访问。

我已为我的 webapi 创建了一个名为“ServerWebApi”的 Azure AD 应用程序,并使用以下应用设置将我的 webapi 应用程序代码配置为在 webConfig 中指向它。

<add key="ida:Tenant" value="myDomainName.onmicrosoft.com" />
<add key="ida:Audience" value="https://myDomainName.onmicrosoft.com/ServerWebApi" />
<add key="ida:ClientID" value="<client id>" />

因此,当请求带有 token 时,它应该使用这些设置根据我的名为 ServerWebApi 的 AD WebApi 应用程序验证 token 。

现在,为了让每个客户端都能够获取 token ,我为每个客户端创建了一个 Azure 应用程序。 (我没有将其创建为 native 应用程序,而是创建为 webapi 应用程序,以便我可以为每个客户端创建单独的 key )。我还将 Azure 中的实际 WebAPI 应用程序 (ServerWebApi) 添加到“此应用程序访问的 Web API”列表中。 (在此示例中,我的第一个客户端称为 myClientWebApiApp)

现在我的客户端可以成功地从 AD 中的客户端应用程序请求 token ,但是当它发布到服务器(webapi)时,我收到 401 - 未经授权。因此,我认为我无法根据 Azure AD ServerWebApi webApi 验证 token 。

我用来获取 token 并调用我的 webApi 的客户端应用程序代码如下

// Create an ADAL AuthenticationContext object and link it to my tennant domain myDomainName.onmicrosoft.com
var authority = ConfigurationManager.AppSettings["Tenant"];
authenticationContext = new AuthenticationContext(authority);

//Client Cerdential Object used while Acquiring a token from Windows Azure AD
ClientCredential clientCred = new ClientCredential(clientId, clientSecret);

// Invoke AuthenticationContext.AcquireToken to obtain an AccessToken. 
// Uses previously-created ClientCredential to authenticate
authenticationResult = authenticationContext.AcquireToken(resource, clientCred);

httpClient.DefaultRequestHeaders.Clear();
//Add authorization header to HttpClient
httpClient.DefaultRequestHeaders.Authorization = new        AuthenticationHeaderValue("Bearer",authenticationResult.AccessToken);

var data = "new Value";

// Call the Service web api to update                    
HttpResponseMessage response = httpClient.PostAsJsonAsync(resourceurl + "api/Values", data).Result;

它使用以下配置设置

<add key="Tenant" value="https://login.windows.net/myDomainName.onmicrosoft.com"/>
<add key="ClientId" value= "<client id>"/>
<add key="ClientSecret" value= "<client secret>"/>
<add key="ResourceUrl" value="https://localhost:44300/"/>
<add key="AppIdUri" value="https://myDomainName.onmicrosoft.com/myClientWebApiApp"/>

客户端 ID 是我创建的客户端 webapi 应用程序的客户端 ID,而 ClientSecret 是我在 Azure AD 中针对该应用程序创建的 key 。

有谁知道我正在做的事情是否可行,如果可以的话我做错了什么?

最佳答案

我相信您的问题最终在于您如何设置 AuthorizationHeader 以及您的 ServerWebApi 如何响应请求。

看看this blog 。我相信这就是您正在寻找的解决方案。

关于c# - Azure Active Directory WebApi - 服务器到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21797154/

相关文章:

visual-studio-2010 - Azure 项目未加载

azure - 如何导入lib WindowsAzure ionic 3

javascript - 如何从 angularJs 连接 signalR

c# - 来自 C# 中嵌套元素的 MongoDB Map 类

c# - Windows 服务应用程序 Controller

c# - Silverlight ComboBox 和 SelectedItem

asp.net - 为什么要在 OAuthAuthorizationServerProvider.GrantRefreshToken 中添加声明?

c# - 如何安排调试?

azure - 通过 terraform 脚本检查 azure-resourcegroup 是否已存在

c# - 使用 Web API 下载大文件返回 OutOfMemoryException