当前位置: 首页 > 知识库问答 >
问题:

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

姜业
2023-03-14

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

我遵循了本指南:https://shellmonger.com/2016/04/08/30-days-of-zumo-v2-azure-mobile-apps-day-5-custom-authentication/ 并进入了“服务器中的自定义JWT验证”阶段,但这是我陷入困境的地方......我的后端在 C# 而不是节点中.js那么如何执行与本教程等效的操作并验证 JWT 令牌,然后使用 AzureClient.login/AzureClient.table 从我的前端应用程序访问表控制器?

编辑:好吧,正如您将在下面的@AdrianHall评论线程中看到的那样,我已经成功地从我的cordova应用程序中生成了一个令牌,但我的绊脚石现在是让服务接受它,而无需交换令牌。根据发布的指南,这是可能的。

这是我的客户端代码,它当前对auth0进行身份验证调用,并进行一些客户端设置以获取用户ID并生成包含新令牌的“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();
    }

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

共有1个答案

洪建茗
2023-03-14

在Azure移动应用程序C#后端中,有一个<code>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()
        });
    }

<代码>应用程序。useappserviceauthentic ation 调用设置解码JWT所需的配置。你只需要了解你的受众(JWT的澳元领域)和发行方(JWT的iss领域)是什么。在auth0的情况下,Audience是您的ClientId,Issuer是“https://your-domain-value”——客户端机密是签名密钥

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

 类似资料:
  • 我们有一个使用社交网络身份验证的Azure移动应用。尝试使用自定义令牌处理程序将用户角色添加为声明。 这一切都可以在localhost上运行——令牌被添加到令牌处理程序中,并且在调用AuthorizationAtinn OnAuthoration方法时可用。具有指定角色的授权属性按预期工作。 但是当运行的是Azure时——声明被添加,但当调用OnAuthorization方法时,自定义角色声明就消

  • 我在决定如何为一个RESTful API实现身份验证时遇到了一些麻烦,该API既可供web应用程序使用,也可供移动应用程序使用。 首先,我想研究HTTPS上的HTTP基本身份验证作为一种选择。对于移动应用程序来说,用户名和密码可以安全地存储在操作系统钥匙链中,并且在传输过程中无法被拦截,因为请求是通过HTTPS发出的。对于API来说,它也很优雅,因为它将是完全无状态的。问题在于web应用程序。将无

  • 在我的网络项目中,我想让用户使用用户名/密码和微软帐户登录。技术-堆栈: Asp.网络核心 角 Azure应用服务 首先,我创建了用户名/密码登录。这样地: 启动。反恐精英: 登录方式: 并通过[授权]保护我的api enpoints。到现在为止,一直都还不错。。。这很有效。 现在我想添加一个登录方法与Microsoft帐户。我使用Azure应用服务身份验证/授权(https://docs.mic

  • 在过去的几周里,我一直在努力掌握KeyClope,这样我就可以实现一种使用遗留提供商(基于Oracle,带有会话表和各种奇怪的东西)对用户进行身份验证的方法。我们计划在不久的将来解决这个问题,但目前我们必须解决这个问题,所以我们的想法是在前线使用KeyClope——利用它提供的主要好处,比如SSO——在需要身份验证的应用程序中省略传统的身份验证提供程序。 我读了一些关于构建自定义OIDC身份提供程

  • 我有一个Keycloak Cordova本地应用程序,一旦启动,就会正确显示Keycloak的用户名/密码表单,但在成功验证后,当试图重定向到该应用程序时,会显示err_connection_rejection。重定向当前设置为“http://localhost”。 我使用了这个例子https://github.com/keycloak/keycloak/tree/master/examples/

  • 问题内容: 这是我的情况: 一个Web应用程序对许多应用程序执行某种SSO 登录的用户,而不是单击链接,该应用就会向正确的应用发布包含用户信息(名称,pwd [无用],角色)的帖子 我正在其中一个应用程序上实现SpringSecurity以从其功能中受益(会话中的权限,其类提供的方法等) 因此,我需要开发一个 自定义过滤器 -我猜想-能够从请求中检索用户信息,通过自定义 DetailsUserSe