需要任何人的帮助。
我有一个 SPA 客户端应用程序正在与 WebApi (.Net 4.52) 通信。两者都是我公司开发的。它与 WebAPI 自己生成的 token 配合得很好(基于 bitoftech 的优秀文章)
我们现在希望允许使用 Azure 进行身份验证 - 更具体地说,是我客户自己的 azure 租户(我无法直接控制)。为了在客户端执行此操作,我们使用 adal.js 来回执行实际登录用户的操作,并在服务器端使用 UseWindowsAzureActiveDirectoryBearerAuthentication 来验证 azure token 。
Oauth 启动看起来像这样:
public static void RunAcademyOAuthStartup(IAppBuilder app)
{
if (ConfigurationManager.AppSettings["ida:Enabled"] == "1")
{
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
{
ValidAudience = ConfigurationManager.AppSettings["ida:Audience"],
AuthenticationType = "ExternalAzureSSO"
},
Tenant = ConfigurationManager.AppSettings["ida:Tenant"]
}
);
}
var apiAccessTokenLifeTimeSeconds = 0;
if (!int.TryParse(ConfigurationManager.AppSettings["ApiAccessTokenLifetimeSeconds"], out apiAccessTokenLifeTimeSeconds))
{
apiAccessTokenLifeTimeSeconds = 1800; //set default to 30 minutes
}
var oAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/api/user/authenticate"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(apiAccessTokenLifeTimeSeconds),
Provider = new SimpleAuthorizationServerProvider(),
RefreshTokenProvider = new SimpleRefreshTokenProvider(),
AuthenticationType = "LocalApplication"
};
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
// Token Generation - setup the token authorization server
app.UseOAuthAuthorizationServer(oAuthServerOptions);
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
}
上面的代码与我几天来一直在寻找的数百个其他示例没有什么不同。
当我使用免费的 Azure 实例(通过我公司的 MSDN)时,这一切都工作正常 - 我可以进行身份验证, token 从 azure 返回,webapi 可以对其进行解码并验证,然后继续。
当我对客户的 P1 Azure 实例尝试此操作时,出现了问题 - 我可以进行身份验证,取回 token ,但我的 webapi 无法解码 token 。据我所知,没有记录任何错误。
目前我能看到的主要内容是,当我获取 azure token 并使用 jwt.io 对其进行解码时,我可以看到“x5t”和“kid”值。根据我在网上找到的其他信息,我应该能够从 https://login.microsoftonline.com/common/discovery/keys 获取最新的 key 。并在那里找到“x5t”和/或“kid”值......但我不能。
这可能吗?什么会导致使用普通 key 以外的 key 生成 token ?这是azure中应用程序注册中的配置项吗?我是否需要向 Web api 中的 UseWindowsAzureActiveDirectoryBearerAuthentication 配置添加更多设置?
token 均为 v1
最佳答案
为将来遇到此线程的任何人提供答案......可能是一个菜鸟错误:)
在我的案例中,我发现 Azure 端的管理员在编辑“应用程序注册”区域中的 list 等之前,已使用其 Azure 门户中的“企业应用程序”选项注册了我的应用程序。根据 Microsoft 支持的说法,应该以相反的方式完成 - 首先在应用程序注册中注册,然后转到企业应用程序(如果需要)。按照该顺序执行操作意味着 azure 租户随后会生成 token 并使用“通用” key (而不是企业应用程序获取的自定义 key )对其进行签名。
关于c# - 使用 UseWindowsAzureActiveDirectoryBearerAuthentication 从 Azure 使用 Id_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53274053/