【问题标题】:Azure Mobile App Service - Custom Authentication - Valid AudiencesAzure 移动应用服务 - 自定义身份验证 - 有效受众
【发布时间】:2017-02-12 16:13:59
【问题描述】:

我已经设置了一个 Azure 移动应用服务后端,并且有一个 Xamarin 应用正在使用它的服务。它使用 Azure 移动应用服务中的自定义身份验证来注册和验证应用的用户。

对于本地开发/调试应用程序的 OWIN 启动类包含一些代码来设置应用服务的身份验证选项,如https://azure.microsoft.com/nl-nl/documentation/articles/app-service-mobile-dotnet-backend-how-to-use-server-sdk/#local-debug 中所述。

在 Azure 中,移动应用服务的身份验证已启用(身份验证/授权),将“请求未通过身份验证时采取的操作”选项设置为“允许请求(无操作)”,以便应用程序处理请求身份验证。

这一切都按预期工作。

现在我们希望在我们的移动应用服务上支持自定义域,并支持当前的 ourmobileappservice.azurewebsites.net 域。我们已经配置了自定义域,配置了它的 SSL 证书并且一切正常。新令牌以自定义域作为受众/发行者发行,并且也在此庄​​园中得到验证。

但是,当以 ourmobileappservice.azurewebsites.net 作为受众/发行者发行令牌时,令牌验证期间会被拒绝。似乎只允许我们的自定义域作为有效受众。

对于本地开发,我们指定app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions { ... }),同时设置ValidAudiences 属性。所以我也想在 Azure 环境中使用这个设置,这样我们就可以指定多个有效的受众来进行令牌验证。 注意:AppServiceAuthenticationOptions 当然与本地开发不同,例如SigningKey 来自 Azure 的环境变量)。

不幸的是,Azure 似乎根本不使用它。它仍然以完全相同的方式失败。如您所见,只有自定义域被指定为有效受众:

警告 JWT 验证失败:IDX10214:受众验证 失败的。观众:'https://ourmobileappservice.azurewebsites.net/'。 不匹配:validationParameters.ValidAudience: 'https://ourcustom.domain.com/' 或 validationParameters.ValidAudiences: 'null'。

如何使用自定义身份验证设置配置 Azure 移动应用服务,使其有效受众支持自定义域作为以前的 ourmobileappservice.azurewebsites.net?

编辑

为 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()
    });

    ...
}

【问题讨论】:

  • 您是否将“ourcustom.domain.com”添加到“ValidAudiences”?
  • 是的,在 ValidAudiences 中指定自定义域和 ourmobileappservice.azurewebsites.net。但是配置的 ValidAudiences 似乎没有得到兑现。
  • 你能分享你用来设置“ValidAudiences”的代码吗?
  • 代码见我帖子的编辑部分。
  • 谢谢,我随便看看就回来了……

标签: azure authentication owin azure-mobile-services


【解决方案1】:

您可以在自定义身份验证提供程序中使用您喜欢的任何 URL 对令牌进行签名。您在 AppServiceLoginHandler.CreateToken() 方法中指定的任何内容都将进入 JWT。

在验证令牌时,如果您在本地调试,中间件中指定的 URL 列表将用于验证受众和颁发者。在生产环境中,Azure 会神奇地使用您的默认 Azure 域和自定义域。

我这样做的方法是创建一个新的 web.config AppSetting,其中包含所有环境的有效签名 URL。

 <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()
    });
}

现在在生成令牌之前的登录方法中,我正在检查当前请求的主机名是否在同一个 AppSetting 白名单中。如果它有效,则使用当前主机名作为我的令牌的受众和发行者。

类似的东西;

    // 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
        );

【讨论】:

    【解决方案2】:

    实际上,我们刚刚进行了一组更新,允许您在门户中设置受众。如果您返回应用服务身份验证/授权下的 AAD 设置,您应该会在“高级”选项卡下看到一些新选项。这包括允许的令牌受众的可编辑列表。

    如果您将 https://ourmobileappservice.azurewebsites.net 添加到该列表中,您应该很高兴。

    【讨论】:

    • 我们没有使用 Azure Active Directory 进行身份验证,而是使用自定义身份验证(管理我们自己的帐户等)。所以我猜这个功能不适用?
    • @mattchenderson - 'Allowed Token Audiences' 是逗号分隔的列表、分号分隔的列表还是.. ?
    • 这应该是我要找的,但是这个功能在哪里记录??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 2017-04-27
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 2015-10-01
    • 2021-10-16
    相关资源
    最近更新 更多