Azure移动应用服务 - 自定义身份验证 - 有效受众

时间:2016-10-04 13:10:23

标签: azure authentication owin azure-mobile-services

我已经设置了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中,启用了移动应用服务的身份验证(身份验证/授权),将“请求未经过身份验证时采取的操作”选项设置为“允许请求(无操作)”,以便应用程序处理请求身份验证。

这一切都符合要求。

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

但是当使用 mymobileappservice .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移动应用服务,以便有效受众支持自定义域,如同以前的 mymobileappservice .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()
    });

    ...
}

2 个答案:

答案 0 :(得分: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
        );

答案 1 :(得分:0)

我们实际上只是制作了一组更新,允许您在门户中设置受众。如果您返回到App Service Authentication / Authorization下的AAD设置,您应该会看到&#34; Advanced&#34;下的一些新选项。标签。这包括允许的令牌受众的可编辑列表。

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

相关问题