title: .Net Core Identity and Azure AD Authentication
body: <p>I have a net core web app with net core identity sign in set up and working, it creates and signs in users stored in our database. My next step was adding Azure Active Directory as an external way of login in and that works fine too, for the most part, I am able to log in. </p>
  
  <p>My issue is that when I added the AAD authentication the identity way of authenticating no longer works to sign me in.</p>
  
  <p>Ideally, the web app would use ADD to authenticate the user but if that fails the user would still have the option to sign in locally to be authenticated. Essentially ADD would be the default sign-in and identity would be the backup.</p>
  
  <p>I've followed the following post's suggestion, since it is very similar if not the same to what I would like my web app to do, by adding AddCookie() to the Startup.cs file but when I do that ADD fails to authenticate me with the message:</p>
  
  <p>"We couldn't sign you in. Please try again."</p>
  
  <p><a href="https://stackoverflow.com/questions/54875422/hybrid-authentication-in-net-core-with-open-id-connect-and-local-database">Hybrid authentication in .net core with Open Id Connect and local database</a></p>
  
  <p>The following is what my Startup.cs file looks like, I've removed the AddCookies() call from the post above so I could get ADD to sign me in again.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public void ConfigureServices(IServiceCollection services)
  {
      services.Configure&lt;CookiePolicyOptions&gt;(options =&gt;
      {
          // This lambda determines whether user consent for non-essential cookies is needed for a given request.
          options.CheckConsentNeeded = context =&gt; true;
          options.MinimumSameSitePolicy = SameSiteMode.None;
      });
  
      services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
      options.UseSqlServer(
          Configuration.GetConnectionString("DefaultConnection")));
  
      services.AddDefaultIdentity&lt;IdentityUser&gt;()
          .AddDefaultUI(UIFramework.Bootstrap4)
          .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;()
          .AddDefaultTokenProviders();
  
      services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
              .AddAzureAD(options =&gt; Configuration.Bind("AzureAd", options));
  
      services.Configure&lt;OpenIdConnectOptions&gt;(AzureADDefaults.OpenIdScheme, options =&gt;
      {
          options.Authority = options.Authority + "/v2.0/";         // Microsoft identity platform
  
          options.TokenValidationParameters.ValidateIssuer = false; // accept several tenants (here simplified)
  
  
      });
  
      services.AddMvc(options =&gt;
      {
          var policy = new AuthorizationPolicyBuilder()
                          .RequireAuthenticatedUser()
                          .Build();
          options.Filters.Add(new AuthorizeFilter(policy));
      })
      .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  }
  </code></pre>
  
  <p>I suspect it may have something to do with the following call:</p>
  
  <pre class="lang-cs prettyprint-override"><code>    services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
              .AddAzureAD(options =&gt; Configuration.Bind("AzureAd", options));
  </code></pre>
  
  <p>Maybe I need to add extra authentication options? I've tried the following but ADD does not authenticate me and I get the same message from ADD:</p>
  
  <p>"We couldn't sign you in. Please try again."</p>
  
  <pre class="lang-cs prettyprint-override"><code>services.AddAuthentication(sharedOptions =&gt;
  {
      sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
      sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
      sharedOptions.DefaultChallengeScheme = AzureADDefaults.AuthenticationScheme;
  })
  .AddAzureAD(options =&gt; Configuration.Bind("AzureAd", options));
  </code></pre>
  
  <p>I am fairly new to authentication and any help is appreciated, thank you.</p>