title: ASP.NET and OWIN Cookies Azure Open ID is not working
body: <p>I have try to use OpenID to connect with Azure AD, and I use the exact code from the tutorial <a href="https://docs.microsoft.com/en-us/azure/active-directory/develop/tutorial-v2-asp-webapp" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/active-directory/develop/tutorial-v2-asp-webapp</a> with no luck.</p>
  <p>My Startup:</p>
  <pre><code>public class Startup
  {
       string clientId = System.Configuration.ConfigurationManager.AppSettings[&quot;ClientId&quot;];
       string redirectUri = System.Configuration.ConfigurationManager.AppSettings[&quot;RedirectUri&quot;];
       static string tenant = System.Configuration.ConfigurationManager.AppSettings[&quot;Tenant&quot;];
       string authority = String.Format(System.Globalization.CultureInfo.InvariantCulture, 
                          System.Configuration.ConfigurationManager.AppSettings[&quot;Authority&quot;], tenant);
  
  /// &lt;summary&gt;
  /// Configure OWIN to use OpenIdConnect
  /// &lt;/summary&gt;
  /// &lt;param name=&quot;app&quot;&gt;&lt;/param&gt;
  public void Configuration(IAppBuilder app)
  {
      app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
  
      app.UseCookieAuthentication(new CookieAuthenticationOptions());
      app.UseOpenIdConnectAuthentication(
          new OpenIdConnectAuthenticationOptions
          {
              ClientId = clientId,
              Authority = authority,
              RedirectUri = redirectUri,
              PostLogoutRedirectUri = redirectUri,
              Scope = OpenIdConnectScope.OpenIdProfile,
              ResponseType = OpenIdConnectResponseType.IdToken,
              TokenValidationParameters = new TokenValidationParameters()
              {
                  ValidateIssuer = true
              },
              
              Notifications = new OpenIdConnectAuthenticationNotifications
              {
                  AuthenticationFailed = OnAuthenticationFailed
              }
          }
      );
  }
  
  /// &lt;summary&gt;
  /// Handle failed authentication requests by redirecting the user to the home page with an error in the query string
  /// &lt;/summary&gt;
  /// &lt;param name=&quot;context&quot;&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  private Task OnAuthenticationFailed(AuthenticationFailedNotification&lt;OpenIdConnectMessage, OpenIdConnectAuthenticationOptions&gt; context)
  {
      context.HandleResponse();
      context.Response.Redirect(&quot;/?errormessage=&quot; + context.Exception.Message);
      return Task.FromResult(0);
  }
  </code></pre>
  <p>My Login Page Code:</p>
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          try
          {
              if (!IsPostBack)
              {
                  if (!Request.IsAuthenticated)
                  {
                      HttpContext.Current.GetOwinContext().Authentication.Challenge(
                          new AuthenticationProperties { RedirectUri = &quot;/AMS/Dashboard&quot; },
                          OpenIdConnectAuthenticationDefaults.AuthenticationType);
                  }
                  else
                  {
                      var userClaims = User.Identity as System.Security.Claims.ClaimsIdentity;
                      lblErrorMessage.InnerHtml = userClaims?.FindFirst(&quot;preferred_username&quot;)?.Value;
                      //check user info, and create session then redirect to Dashboard
                  }
              }
          }
          catch (Exception ex)
          {
               //handle error
          }
    }
  </code></pre>
  <p>My Website structure is a little bit complicated as follow:</p>
  <p>I have a website on the server x: mydomain.com
  and I have a subdomain in server y: subdomain.mydomain.com
  and I have my website AMS on server z with a redirect to subdomain.mydomain.com/AMS</p>
  <p>now to solve cross-site cookie I use the following in web config</p>
  <pre><code>&lt;outboundRules&gt;
      &lt;rule name=&quot;Ensure httpOnly Cookies&quot; preCondition=&quot;Missing httpOnly cookie&quot;&gt;
          &lt;match serverVariable=&quot;RESPONSE_Set_Cookie&quot; pattern=&quot;^(.*; path=/)&quot; negate=&quot;false&quot; /&gt;
          &lt;action type=&quot;Rewrite&quot; value=&quot;{R:1}AMS; SameSite=none; secure; HttpOnly&quot; /&gt;
      &lt;/rule&gt;
      &lt;preConditions&gt;
          &lt;preCondition name=&quot;Missing httpOnly cookie&quot;&gt;
              &lt;!-- Don't remove the first line! --&gt;
              &lt;add input=&quot;{RESPONSE_Set_Cookie}&quot; pattern=&quot;.&quot; /&gt;
              &lt;add input=&quot;{RESPONSE_Set_Cookie}&quot; pattern=&quot;; SameSite=none; secure; HttpOnly&quot; negate=&quot;true&quot; /&gt;
          &lt;/preCondition&gt;
      &lt;/preConditions&gt;
  &lt;/outboundRules&gt;
  </code></pre>
  <p><strong>My Problem is Request.IsAuthenticated is always false, so the page keep redirect to the Microsoft login page</strong></p>
  <p>Any ideas?
  Thanks in advance</p>