2016-07-14 92 views
2

我已將Web API服務添加到「遺留」MVC項目(而不是vNext),仍然使用Membership和窗體身份驗證模塊。 一切正常,直到我決定使用適用於Azure App Services for Mobile的最新SDK開發Web api移動服務爲止。Azure應用服務移動身份驗證與MVC/Forms不兼容

我迄今收窄問題這個

//app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions() 
      //{ 

      // SigningKey = CloudConfigurationManager.GetSetting("authSigningKey"), 
      // ValidAudiences = new[] { CloudConfigurationManager.GetSetting("authAudience") }, 
      // ValidIssuers = new[] { CloudConfigurationManager.GetSetting("authIssuer") }, 
      // TokenHandler = GlobalConfiguration.Configuration.GetAppServiceTokenHandler() 
      //}); 

這改變了應用程序的其餘部分,以便MVC +窗體身份驗證不起作用。沒時間研究這個問題。 任何線索? UseCookieAuthenticationUseExternalSignInCookieUseOAuthAuthorizationServer調用UseAppServiceAuthentication之前 在你Startup寄存器電話:

+0

得到了同樣的問題。你能找到解決方案嗎? –

回答

1

下面的解決方案是爲我工作。

第二步: 添加下面的類到您的項目:

private sealed class CustomAppServiceAuthenticationMiddleware : AppServiceAuthenticationMiddleware 
{ 
    private readonly ILogger _logger; 

    public CustomAppServiceAuthenticationMiddleware(OwinMiddleware next, IAppBuilder appBuilder, AppServiceAuthenticationOptions options) : base(next, appBuilder, options) 
    { 
     _logger = (ILogger)GetType().BaseType.GetField("logger", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this); 
    } 

    protected override AuthenticationHandler<AppServiceAuthenticationOptions> CreateHandler() 
    { 
     return new AppServiceAuthenticationHandler(_logger); 
    } 

    public override Task Invoke(IOwinContext context) 
    { 
     string logLine = $"AppServiceAuthMiddleware: {context.Request.Path}"; 

     if (context.Request.Headers.TryGetValue("Authorization", out var values)) 
      logLine += $"; Authorization: {values.First().Split(' ').FirstOrDefault()}"; 
     if (context.Request.Headers.TryGetValue("X-ZUMO-AUTH", out values)) 
      logLine += $"; X-ZUMO-AUTH: {values.First()}"; 

     _logger.WriteVerbose(logLine); 
     Debug.WriteLine(logLine); 

     if (IsZumoAuth(context)) 
     { 
      return base.Invoke(context); 
     } 

     return Next.Invoke(context); 
    } 

    private bool IsZumoAuth(IOwinContext context) 
    { 
     return context.Request.Headers.ContainsKey("X-ZUMO-AUTH"); 
    } 
} 

THRID步: 更換app.UseAppServiceAuthentication有以下幾點:

 app.Use(typeof(CustomAppServiceAuthenticationMiddleware), app, new AppServiceAuthenticationOptions 
     { 
      SigningKey = ..., 
      ValidAudiences = ..., 
      ValidIssuers = ..., 
      TokenHandler = ... 
     }); 

這將owin pipline使調用AppServiceAuthenticationMiddleware只爲ZUMO-AUTH驗證。

我有一個混合的網絡&移動應用程序。 使用此方法,Web應用程序的成員身份驗證正在運行。 在應用程序中,一些自定義oauth(基於刷新令牌)加上azure auth(facebook,google,...)也在工作。所有在同一個asp.net應用程序中。

+0

這太棒了。 – Sentinel

相關問題