asp.net - 应用程序之间的 Azure AD 身份验证

标签 asp.net asp.net-mvc azure active-directory azure-active-directory

我有一个应用程序,我们将其命名为“Apple”,该应用程序已在 Azure AD 中注册,拥有 Azure 管理 API 应用程序的委派权限。当向该应用程序发出请求时,它会创建 azure 资源。存储帐户会自动运行,效果很好。

我有另一个应用程序,它是 MVC 应用程序,它也注册到相同的 AD 租户。第二个应用程序使用以下代码来检索访问 token :

 var clientCredentials = new ClientCredential(ConfigurationManager.AppSettings["AD_ClientID"], ConfigurationManager.AppSettings["AD_Client_AccessKey"]);
 var authContext = new AuthenticationContext(string.Format(ConfigurationManager.AppSettings["AD_Tenant_Login_Url"], ConfigurationManager.AppSettings["AD_Tenant_Id"]));            
 var result = authContext.AcquireTokenAsync(ConfigurationManager.AppSettings["AD_Resource"], clientCredentials);
 if (result == null)
 {
     throw new InvalidOperationException("Could not get the token");
 }
 return result.Result;

结果是具有不同属性的访问 token 。现在,第二个应用程序检索具有对资源 apple 的访问权限的访问 token ,然后将其传递到授权 header 中的 Apple 应用程序。

Authorization:bearer TokenString

Apple 应用程序已将 Authorize 属性添加到 Controller 。 该应用程序使用 Owin 和 oauth 应用程序进行配置,代码如下

public void ConfigureAuth(IAppBuilder app)
    {
        app.UseWindowsAzureActiveDirectoryBearerAuthentication(
            new WindowsAzureActiveDirectoryBearerAuthenticationOptions
            {
                Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
                TokenValidationParameters = new TokenValidationParameters
                {

                    ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
                },
            });
    }

请注意,访问 token 是使用第二个应用程序自己的 AppId 和 key 检索的;而另一个(Apple)应用程序使用自己的 AppId 和 key 来验证 token 。

所以我的问题是,APPLE应用程序总是返回401未授权代码

最佳答案

对于上面的问题,答案是,资源 ID(在 token 请求期间)和受众 ID(在第二个应用程序中验证 token 期间)不匹配。保持这些相同解决了问题。

然后我遇到了另一个问题,我已经描述了 here

看来,如果我使用较新的 Azure 门户(仍处于预览版本),AD token不包括"Roles"领域 JWT token 。如果我在旧门户中遵循相同的过程来配置应用程序,则 AD 包括 "Roles"领域 JWT token 和场景按预期执行。

我至少应该避免使用 Azure 新门户来实现预览功能!

关于asp.net - 应用程序之间的 Azure AD 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39672739/

相关文章:

vb.net - RetryPolicy.Retrying 事件在 SQL Azure 的 transient 错误处理期间未触发?

javascript - 在 ASP.NET MVC5 中向表单动态添加控件

c# - 在 Session ASP .NET MVC 中像纯文本一样存储密码

azure - 应用程序服务应用程序设置未被覆盖

jquery - 如何在 Kendo UI 网格中获得行号

jquery - 在应用程序中将 jQuery 和 YUI 混合在一起,这很容易实现吗?

python - 从python中的azure blob存储读取文件

asp.net - 调用网络服务 "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel"

c# - 如何在 ASP.NET Web api 中获取客户端请求的 url?

c# - 指定的转换无效