简体   繁体   English

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

[英]Azure mobile apps custom authentication with Cordova

I currently have a backend solution for my app using Azure Mobile Apps. 我目前使用Azure移动应用程序为我的应用程序提供后端解决方案。 I have enabled facebook, twitter, google and Microsoft logins. 我启用了facebook,twitter,google和Microsoft登录。 I am attempting to add a custom login flow in addition to this. 我正在尝试添加自定义登录流程。 I have setup an Auth0 account and application and I am able to get a token and profile back from auth0 when I make the request in-app using auth0 lock widget. 我已经设置了一个Auth0帐户和应用程序,当我使用auth0 lock widget在应用程序中发出请求时,我能够从auth0获取令牌和配置文件。

I followed this guide: https://shellmonger.com/2016/04/08/30-days-of-zumo-v2-azure-mobile-apps-day-5-custom-authentication/ and got to the stage 'Custom JWT Verification in the Server' but this is where I am stuck...my backend is in C# not node.js so how do I do the equivalent to this tutorial and validate the JWT token and subsequently access the table controllers from my front end application using azureClient.login/azureClient.table? 我遵循了这个指南: https//shellmonger.com/2016/04/08/30-days-of-zumo-v2-azure-mobile-apps-day-5-custom-authentication/并进入舞台'Custom服务器中的JWT验证'但这是我被卡住的地方......我的后端是在C#而不是node.js,那么我该如何做与本教程相同的操作并验证JWT令牌,然后从我的前端访问表控制器使用azureClient.login / azureClient.table的应用程序?

EDIT: Okay so as you will see in the comment thread below with @AdrianHall I have been successful in generating a token from within my cordova app but my stumbling block is now getting the service to accept it without having to exchange tokens. 编辑:好的,所以你会在@AdrianHall的评论主题中看到我已经成功地从我的cordova应用程序中生成一个令牌,但我的绊脚石现在让服务接受它无需交换令牌。 This is possible according to the guide posted. 根据发布的指南,这是可能的。

This is my client-side code which currently makes the auth call to auth0 and does some client side set up to get a userID and generate the 'currentUser' object containing the new token. 这是我的客户端代码,它当前对auth0进行auth调用,并设置一些客户端来获取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();
    }

Backend code MobileAppSettingsDictionary 后端代码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()
             });
        }

In the Azure Mobile Apps C# backend, there is an App_Start\\Startup.Mobile.cs file with the following code: 在Azure Mobile Apps 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()
        });
    }

The app.UseAppServiceAuthentication call sets up the configuration needed for decoding your JWT. app.UseAppServiceAuthentication调用设置解码JWT所需的配置。 You just need to understand what your Audience (the aud field in the JWT) and Issuer (the iss field in the JWT). 您只需要了解您的受众(JWT中的aud字段)和Issuer(JWT中的iss字段)。 In the auth0 case, Audience is your ClientId and Issuer is " https://your-domain-value " - the Client Secret is the signing key 在auth0情况下,Audience是您的ClientId,Issuer是“ https:// your-domain-value ” - Client Secret是签名密钥

You can verify an example JWT by cut-and-paste at https://jwt.io - this will show explicitly what the values should be and allow you to verify the signature. 您可以通过https://jwt.io上的剪切和粘贴来验证示例JWT - 这将明确显示值应该是什么,并允许您验证签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM