【问题标题】:Azure mobile apps Custom + Facebook authentication with Xamarin.FormsAzure 移动应用自定义 + Facebook 身份验证与 Xamarin.Forms
【发布时间】:2017-03-09 00:16:36
【问题描述】:

我正在开发一个带有 .NET 后端的 Xamarin Forms 移动应用程序。我关注this guide 并成功设置了自定义身份验证,并在 Startup.cs 中进行了一次更改:

app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
        {
            SigningKey = Environment.GetEnvironmentVariable("WEBSITE_AUTH_SIGNING_KEY"),
            ValidAudiences = new[] { Identifiers.Environment.ApiUrl },
            ValidIssuers = new[] { Identifiers.Environment.ApiUrl },
            TokenHandler = config.GetAppServiceTokenHandler()
        });

没有“if (string.IsNullOrEmpty(settings.HostName))”。否则,我总是在登录后对所有请求都未经授权。

服务器项目:

  1. 授权控制器

    公共类 ClubrAuthController : ApiController { 私有只读 ClubrContext dbContext; 私有只读 ILoggerService loggerService;

    public ClubrAuthController(ILoggerService loggerService)
    {
        this.loggerService = loggerService;
        dbContext = new ClubrContext();
    }
    
    public async Task<IHttpActionResult> Post(LoginRequest loginRequest)
    {
        var user = await dbContext.Users.FirstOrDefaultAsync(x => x.Email == loginRequest.username);
        if (user == null)
        {
            user = await CreateUser(loginRequest);
        }
    
        var token = GetAuthenticationTokenForUser(user.Email);
    
        return Ok(new
        {
            authenticationToken = token.RawData,
            user = new { userId = loginRequest.username }
        });
    }
    
    private JwtSecurityToken GetAuthenticationTokenForUser(string userEmail)
    {
        var claims = new[]
        {
            new Claim(JwtRegisteredClaimNames.Sub, userEmail)
        };
    
        var secretKey = Environment.GetEnvironmentVariable("WEBSITE_AUTH_SIGNING_KEY");
        var audience = Identifiers.Environment.ApiUrl;
        var issuer = Identifiers.Environment.ApiUrl;
    
        var token = AppServiceLoginHandler.CreateToken(
            claims,
            secretKey,
            audience,
            issuer,
            TimeSpan.FromHours(24)
            );
    
        return token;
    }
    

    }

  2. Startup.cs

        ConfigureMobileAppAuth(app, config, container);
        app.UseWebApi(config);
    }
    
    private void ConfigureMobileAppAuth(IAppBuilder app, HttpConfiguration config, IContainer container)
    {
        config.Routes.MapHttpRoute("ClubrAuth", ".auth/login/ClubrAuth", new { controller = "ClubrAuth" });
    
        app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
        {
            SigningKey = Environment.GetEnvironmentVariable("WEBSITE_AUTH_SIGNING_KEY"),
            ValidAudiences = new[] { Identifiers.Environment.ApiUrl },
            ValidIssuers = new[] { Identifiers.Environment.ApiUrl },
            TokenHandler = config.GetAppServiceTokenHandler()
        });
    }
    

客户项目:

MobileServiceUser user = await MobileClient.LoginAsync(loginProvider, jtoken);

此外,我在 azure 门户中配置了 Facebook 提供程序,如 here 所述。 但只有当我注释掉 app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions(){...});在 Startup.cs 中。 让两种类型的身份验证同时工作我缺少什么?

【问题讨论】:

    标签: authentication xamarin.forms azure-mobile-services


    【解决方案1】:

    由于您启用了应用服务身份验证/授权,这将验证令牌。它假定有关您的令牌结构的事情,例如让受众和发行者与您的应用 URL 相同(作为默认值)。

    app.UseAppServiceAuthentication() 也将验证令牌,因为它是meant for local development。因此,在您的示例中,令牌将被验证两次。除了潜在的性能影响外,这通常很好。但是,这意味着令牌必须在两个层上都通过验证,我怀疑情况并非如此,因此会出现错误。

    检查这一点的一种方法是检查令牌本身。在您的客户端应用程序中设置断点并获取您从LoginAsync() 获得的令牌,该令牌将成为该用户对象的一部分。然后前往http://jwt.io 之类的服务,查看令牌内容的样子。我怀疑 Facebook 令牌的 aud 和 iss 声明与您为 app.UseAppServiceAuthentication() 配置的 Identifiers.Environment.ApiUrl 声明不同,而自定义令牌可能会匹配它,因为您在第一个代码中使用了该值sn-p。

    如果这是真的,那么您应该处于两个令牌都失败的状态。 Facebook 令牌将通过托管验证但在本地中间件上失败,而自定义令牌将在托管验证失败但通过本地中间件。

    这里最简单的解决方案是在云中托管时删除 app.UseAppServiceAuthentication()。您还需要确保您对 CreateToken() 的调用使用基于云的 URL 作为受众和发布者。


    对于发现此问题的其他人

    • 可以在here找到自定义身份验证的文档。
    • 可以在here 找到应用服务身份验证/授权的一般概述。

    【讨论】:

      【解决方案2】:

      您引用的代码仅适用于本地部署。对于 Azure 部署,您需要打开应用服务身份验证/授权 - 即使您没有配置身份验证提供程序(在自定义身份验证的情况下您不会这样做)。

      查看我的书的第 2 章 - http://aka.ms/zumobook

      【讨论】:

      • 我已打开应用服务身份验证/授权,但在没有 app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions(){...});在 Startup.cs 中。是否可以使用 Xamarin.Forms 配置自定义 + Facebook 身份验证?有人可以提供工作样本吗?
      • 这是可能的。 Adrian 指出的是 app.UseAppServiceAuthentication() 与自定义身份验证部分无关,我认为它在这里引入了问题。将在我自己的答案中解释更多,以免达到字符限制:)
      猜你喜欢
      • 2016-12-03
      • 2017-04-27
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 2014-03-12
      • 2017-02-12
      • 1970-01-01
      相关资源
      最近更新 更多