2016-07-14 91 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

相关问题