Azure active directory和owin身份验证

Azure active directory和owin身份验证,azure,owin,azure-active-directory,katana,Azure,Owin,Azure Active Directory,Katana,刚刚在azure ad应用程序和owin openid身份验证方面遇到了一个奇怪的问题。 复制该问题 1.选择云应用程序模板,在vs 2015中创建具有azure ad身份验证的web应用程序 2.让标准代码保持原样 3.让startup.auth保持原样 4.在本地运行该应用程序,效果良好 5.现在将startupáuth中的代码更改如下 public partial class Startup { private static string clientId = Configurat

刚刚在azure ad应用程序和owin openid身份验证方面遇到了一个奇怪的问题。 复制该问题

1.选择云应用程序模板,在vs 2015中创建具有azure ad身份验证的web应用程序

2.让标准代码保持原样

3.让startup.auth保持原样

4.在本地运行该应用程序,效果良好

5.现在将startupáuth中的代码更改如下

public partial class Startup
{
    private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
    private static string appKey = ConfigurationManager.AppSettings["ida:ClientSecret"];
    private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
    private static string tenantId = ConfigurationManager.AppSettings["ida:TenantId"];
    private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];

    public static readonly string Authority = aadInstance + tenantId;

    // This is the resource ID of the AAD Graph API.  We'll need this to request a token to call the Graph API.
    string graphResourceId = "https://graph.windows.net";

    private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    public void ConfigureAuth(IAppBuilder app)
    {
        ApplicationDbContext db = new ApplicationDbContext();

        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
        logger.Debug("SetDefaultSignInAsAuthenticationType called");
        //app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseCookieAuthentication(
        new CookieAuthenticationOptions
        {
            Provider = new CookieAuthenticationProvider
            {
                OnResponseSignIn = ctx =>
                {
                    //logger.Debug("OnResponseSignIn called");
                    ////ctx.Identity = TransformClaims(ctx.Identity);
                    //logger.Debug("TransformClaims called");
                }
            }
        });

app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = Authority,
                PostLogoutRedirectUri = postLogoutRedirectUri,

                Notifications = new OpenIdConnectAuthenticationNotifications()
                {
                    // If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
                   AuthorizationCodeReceived = (context) =>
                   {
                       var code = context.Code;
                       ClientCredential credential = new ClientCredential(clientId, appKey);
                       string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
                       logger.Debug("OnResponseSignIn called");
                       logger.Debug("signedInUserID =" + signedInUserID);
                       TransformClaims(context.AuthenticationTicket.Identity);
                       logger.Debug("TransformClaims called");
                       AuthenticationContext authContext = new AuthenticationContext(Authority, new ADALTokenCache(signedInUserID));
                       AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
                       code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);



                       return Task.FromResult(0);
                   },



                    // we use this notification for injecting our custom logic
                    SecurityTokenValidated = (context) =>
                    {
                        logger.Debug("SecurityTokenReceived called");
                        //TransformClaims();  //pass the identity
                        return Task.FromResult(0);
                    },


                }
            });
    }


    private static void TransformClaims(System.Security.Claims.ClaimsIdentity identity)
    {
        if (identity != null && identity.IsAuthenticated == true)
        {
            var usserobjectid = identity.FindFirst(ConfigHelpers.Azure_ObjectIdClaimType).Value;
                ((System.Security.Claims.ClaimsIdentity)identity).AddClaim(new System.Security.Claims.Claim("DBID", "999"));
                ((System.Security.Claims.ClaimsIdentity)identity).AddClaim(new System.Security.Claims.Claim("Super","True"));
        }

        // return identity;
    }

}
6.在本地运行该应用程序,它将完美运行

7.在azure网站上部署应用程序,将永远不会调用startupáuth owin通知方法。但是,应用程序可以工作,但身份转换不能

有人能帮个忙吗这是azure广告应用程序不支持cookies或通知不启动或代码有任何问题


只是为了重新断言startup.uth以外的代码,没有标准代码被更改

我知道这有点过时,但最近我遇到了完全相同的问题,我花了几个小时试图理解为什么它在Azure中不起作用,但在我的localhost中却工作得非常好

这基本上是一个配置问题:在portal.azure.com中选择您的应用程序,然后转到设置>身份验证/授权,并确保关闭应用程序服务身份验证

事实证明,此设置将接管startup.auth设置


我必须完全相信维托里奥·贝尔托西(Vittorio Bertocci)向我指出的这一点。

尝试删除logger.Debug(“OnResponseSignIn called”);然后再次部署。有时trace.writes在Azure中托管时可能会导致问题,具体取决于跟踪侦听器是什么…我添加了logger.debug(“OnResponseSignin Called”)来记录所有事件是否触发,如果这可能是问题所在,那么它在本地主机上的功能可能不会太完美,所以我添加了log4net logger,只想知道发生了什么,但它在本地主机上运行得很好,而不是在azure网站上。所以我相信,这不是问题所在。你可以复制它,我已经通过了完整的代码。这将在本地工作,但在azure中它不会。。。试着做远程调试,看看哪里出了问题……做了所有的准备工作,在所有的尝试之后,最后发布,看看哪里可能出了问题。记录器不是问题,伊戈尔。问题是不会触发通知,此后也不会发生标识转换。唯一可以防止触发通知的是,身份验证过程没有走那么远(例如,早期出现了一些错误)。但是你声称登录可以正常工作?其中一个应该是错误的。尝试连接所有通知以查看进程的进展情况。您还可以通过执行一些更引人注目的操作(如抛出自定义异常)来证明通知触发。