在Azure中,移动应用服务的身份验证已启用(身份验证 /授权)设置“ tosaction”在请求未经验证时要采取的“ toaction”选项“允许请求(无操作)”,因此应用程序应用程序处理请求身份验证。
这一切都根据需要起作用。 现在,我们想在移动应用服务上支持自定义域,并支持当前的ourmobileappservice
.azurewebsites.net域。我们已经配置了自定义域,配置了它的SSL证书,并且一切都很好。新令牌以自定义域名作为受众/发行人发行,并且在此庄园中也得到了验证。但是,在使用ourmobileappservice.azurewebsites.net作为听众/发行人发行令牌时,它在令牌验证期间被拒绝。似乎只有我们的自定义域名作为有效的受众。
对于本地开发,我们要指定app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions { ... })
ValidAudiences
属性。因此,我也想将此设置用于Azure环境,因此我们可以为令牌验证指定多个有效的受众。 注:当然,
AppServiceAuthenticationOptions
与本地发展不同,例如SigningKey来自Azure的环境变量)。 不幸的是,Azure似乎根本没有使用它。它仍然以完全相同的方式失败。如您所见,只有自定义域被指定为有效受众:
验证JWT验证失败:IDX10214:受众验证 失败的。受众:'https://ourmobileappservice.azurewebsites.net/
'。 没有匹配:验证帕拉梅特。 'https://ourcustom.domain.com/
'or 验证parameters.validaudiences:'null'.
如何使用自定义身份验证设置配置Azure移动应用程序服务,以便有效的受众支持自定义域,因为以前eDit
为Azure指定有效的受众,如下:
我这样做的方法是创建一个新的Web.config AppSetting,其中包含所有环境的有效签名URL。当您在当地调试中间软件中指定的URL列表时,将用于验证受众和发行人时,当您在本地进行验证时,请进行验证。在生产中,Azure将神奇地使用您的默认Azure域和自定义域。
public static void ConfigureMobileApp(IAppBuilder app) { ... app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions { SigningKey = Environment.GetEnvironmentVariable("WEBSITE_AUTH_SIGNING_KEY"), ValidAudiences = new[] { "https://ourcustom.domain.com/", "https://ourmobileappservice.azurewebsites.net/" }, ValidIssuers = new[] { "https://ourcustom.domain.com/", "https://ourmobileappservice.azurewebsites.net/" }, TokenHandler = config.GetAppServiceTokenHandler() }); ... }
您可以在自定义验证提供商中使用任何您喜欢的URL签名令牌。您在AppServiceLoginHandler.CreateToken()方法中指定的内容将进入JWT.
<add key="ValidUrls" value="https://api.myproductiondomain.com/, https://myproductionapp.azurewebsites.net/, http://localhost:59475/" />
在startup.mobillapp.cs中,我从此列表中填充有效的受众和发行人。
MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings();
if (string.IsNullOrEmpty(settings.HostName))
{
// This middleware is intended to be used locally for debugging. By default, HostName will
// only have a value when running in an App Service application.
var validUrls = ConfigurationManager.AppSettings["ValidUrls"].Split(',').Select(u => u.Trim()).ToList();
app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
{
SigningKey = ConfigurationManager.AppSettings["SigningKey"],
ValidAudiences = validUrls,
ValidIssuers = validUrls,
TokenHandler = config.GetAppServiceTokenHandler()
});
}
在生成令牌之前,在我的登录方法中,我正在检查当前请求的主机名是否在同一应用程序中。如果有效,请使用当前主机名作为我的令牌的受众和发行人。 像这样的东西;
// Get current URL
var signingUrl = $"{this.Request.RequestUri.Scheme}://{this.Request.RequestUri.Authority}/";
// Get list from AppSetting
var validUrls = ConfigurationManager.AppSettings["ValidUrls"].Split(',').Select(u => u.Trim()).ToList();
// Ensure current url is in whitelist
if (!validUrls.Contains(signingUrl))
{
return this.Request.CreateUnauthorizedResponse();
}
var claims = new Claim[]
{
new Claim(JwtRegisteredClaimNames.Sub, user.Id),
};
var signingKey = this.GetSigningKey();
// Sign token with this
var audience = signingUrl;
var issuer = signingUrl;
// Set expirey
var expiry = TimeSpan.FromHours(72);
// Generate token
JwtSecurityToken token = AppServiceLoginHandler.CreateToken(
claims,
signingKey,
audience,
issuer,
expiry
);
https://ourmobileappservice.azurewebsites.net
在该列表中,您应该很好。