Azure移动应用程序使用Cordova进行自定义身份验证

7
我目前有一个后端解决方案,使用Azure Mobile Apps。我已经启用了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的评论线程下看到的那样,我成功地从我的cordova应用程序内部生成了令牌,但我的绊脚石是使服务接受它而无需交换令牌。根据发布的指南,这是可能的。
这是我的客户端代码,目前调用auth0并进行一些客户端设置,以获取userID并生成包含新令牌的“currentUser”对象。
 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();
    }

后端代码 MobileAppSettings字典

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

感谢您的编辑。遗憾的是,您无法为赏金提供答案。 - anthonyhumphreys
1个回答

5
在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 所需的配置。你只需了解你的受众(JWT 中的 aud 字段)和发行者(JWT 中的 iss 字段)是什么即可。对于 auth0,受众是你的 ClientId,发行者是 "https://your-domain-value" - 客户端密钥是签名密钥。
你可以在 https://jwt.io 上剪切并粘贴示例 JWT,这将明确显示应该是什么值,并允许你验证签名。

还有一个问题 - 我应该从前端向 Azure 发出什么调用来进行授权? - anthonyhumphreys
1
@anthonyhumphreys 请查看将身份验证添加到您的Apache Cordova应用程序 - lindydonna
1
我建议您在Auth0中启用FB、Twitter等社交媒体登录,这样可以使用单一的身份验证提供者。 - Adrian Hall
现在这个程序已经可以正常工作了,但是当我在控制器上启用[Authorize]时,会收到401错误。令牌方面的问题似乎已经解决了,但他们无法通过我的控制器进行身份验证。 - anthonyhumphreys
1
您需要进行自定义身份验证,以将Auth0令牌替换为ZUMO-AUTH令牌。请参阅以下链接了解一般流程:https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter2/custom/#using-third-party-tokens - 您需要将其翻译为Cordova版本,而不是.NET版本。 - Adrian Hall
显示剩余8条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接