【问题标题】:Azure mobile apps custom authentication with Cordova使用 Cordova 的 Azure 移动应用自定义身份验证
【发布时间】:2016-11-05 06:49:22
【问题描述】:

我目前有一个使用 Azure 移动应用程序的应用程序的后端解决方案。我已启用 facebook、twitter、google 和 Microsoft 登录。除此之外,我还尝试添加自定义登录流程。我已经设置了一个 Auth0 帐户和应用程序,当我使用 auth0 锁定小部件在应用程序内发出请求时,我能够从 auth0 获取令牌和配置文件。

我遵循了本指南:https://shellmonger.com/2016/04/08/30-days-of-zumo-v2-azure-mobile-apps-day-5-custom-authentication/ 并进入了“服务器中的自定义 JWT 验证”阶段,但这就是我被卡住的地方...我的后端是 C# 而不是 node.js,所以我该怎么做阅读本教程并验证 JWT 令牌,然后使用 azureClient.login/azureClient.table 从我的前端应用程序访问表控制器?

编辑:好的,正如您将在下面的@AdrianHall 评论线程中看到的那样,我已经成功地从我的科尔多瓦应用程序中生成了一个令牌,但我的绊脚石现在让服务接受它没有 em> 必须交换代币。根据发布的指南,这是可能的。

这是我的客户端代码,它当前对 auth0 进行身份验证调用,并进行一些客户端设置以获取用户 ID 并生成包含新令牌的“当前用户”对象。

 auth0.lock.show(auth0.options, function(err, profile, token) {
    if (err) {
     console.error('Error authenticating with Auth0: ', err);
     alert(err);
    } else {
     debugger;
     var userID;
     if (profile.user_id.indexOf("auth0") > -1) {
      userID = profile.user_id.replace("auth0|", "");
     } else if (profile.user_id.indexOf("facebook") > -1) {
      userID = profile.user_id.replace("facebook|", "");
     } else if (profile.user_id.indexOf("twitter") > -1) {
      userID = profile.user_id.replace("twitter|", "");
     } else if (profile.user_id.indexOf("microsoft") > -1) {
      userID = profile.user_id.replace("microsoft|", "");
     } else if (profile.user_id.indexOf("google-oauth2") > -1) {
      userID = profile.user_id.replace("google-oauth2|", "");
     }
     window.azureClient.currentUser = {
      userId: userID,
      profile: profile,
      mobileServiceAuthenticationToken: token
     };

     //A client session has now been created which contains attributes relevant to the currently logged in user.

     console.log("window.azureClient.currentUser", window.azureClient.currentUser);
     window.localStorage.setItem("currentUser", JSON.stringify(window.azureClient.currentUser));
     //Call the get profile function which will call our API to get the user's activities and bio etc.
     getProfile();
    }

后端代码 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.
            app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
            {
                SigningKey = ConfigurationManager.AppSettings[""],
                ValidAudiences = new[] { ConfigurationManager.AppSettings[""] },
                ValidIssuers = new[] { ConfigurationManager.AppSettings["https://domain.eu.auth0.com/"] },
                TokenHandler = config.GetAppServiceTokenHandler()
             });
        }

【问题讨论】:

  • 感谢您的编辑。很遗憾您无法为赏金提供答案。

标签: c# cordova azure-mobile-services auth0


【解决方案1】:

在 Azure 移动应用 C# 后端,有一个 App_Start\Startup.Mobile.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.
        app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
        {
            SigningKey = ConfigurationManager.AppSettings["SigningKey"],
            ValidAudiences = new[] { ConfigurationManager.AppSettings["ValidAudience"] },
            ValidIssuers = new[] { ConfigurationManager.AppSettings["ValidIssuer"] },
            TokenHandler = config.GetAppServiceTokenHandler()
        });
    }

app.UseAppServiceAuthentication 调用设置解码 JWT 所需的配置。您只需要了解您的 Audience(JWT 中的 aud 字段)和 Issuer(JWT 中的 iss 字段)是什么。在 auth0 的情况下,Audience 是您的 ClientId,Issuer 是“https://your-domain-value” - Client Secret 是签名密钥

您可以通过在https://jwt.io 上剪切和粘贴来验证示例 JWT - 这将明确显示值应该是什么并允许您验证签名。

【讨论】:

  • 哦,这个人自己真是太棒了。非常感谢你的博客——它对社区来说是一笔巨大的财富:)我明天会看看你所说的。感谢您的帮助。
  • 还有一个问题 - 我应该从前端打什么电话到 azure 来授权?
  • 我建议您改为在 Auth0 中启用 FB、Twitter 等 - 这允许您使用单个身份验证提供程序。
  • 您需要进行自定义身份验证才能将 Auth0 令牌换成 ZUMO-AUTH 令牌。有关一般过程,请参阅此内容:adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/… - 您必须转换为 cordova 版本而不是 .NET 版本。
猜你喜欢
  • 2017-04-27
  • 2014-12-08
  • 2018-03-19
  • 2017-03-09
  • 2015-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多