title: ASP.NET with OpenIdAuthentication: redirect to url if not authorized
body: <p>I am attempting to write an ASP.NET application that uses a hybrid authentication scheme.
  A user can either have his username and password hash stored in the UserStore, or he can authenticate via Azure Active Directory.</p>
  
  <p>I have created the login form pictured. It has the standard <code>UserName</code> and <code>Password</code> inputs, but also has a "Login via Active Directory" button.</p>
  
  <p><a href="https://i.stack.imgur.com/pYc8C.png" rel="noreferrer"><img src="https://i.stack.imgur.com/pYc8C.png" alt="enter image description here"></a></p>
  
  <p>This works well.</p>
  
  <p>Now for the problem: The application's home page has the <code>[Authorize]</code> attribute. </p>
  
  <pre><code>public class DefaultController : Controller
  {
      [Authorize]
      public ViewResult Index()
      { 
      // Implementation
      }
  }
  </code></pre>
  
  <p>If the user is not logged in, I want it to redirect to the page <code>Account/Login</code>, allowing the user to choose the authentication method.</p>
  
  <p>Once I added <code>IAppBuilder.UseOpenIdConnectAuthentication</code> to the pipeline setup, it no longer redirects to that page. Instead, it goes straight to the Microsoft Login page.</p>
  
  <p>How do I configure it so that OpenID authentication is part of the system, but allow me to specify how to perform redirections when the user is not authenticated?</p>
  
  <p>Here's the code where I set up the pipeline:</p>
  
  <pre><code>appBuilder.SetDefaultSignInAsAuthticationType(CookieAuthenticationDefaults.AuthenticationType_;
  var cookieAuthenticationOptions = new CookieAuthenticationOptions
  {
       AuthenticationType = DefaultAuthenticationType.ApplicationCookie,
       LoginPath = new Microsoft.Owin.PathString("/Account/Login"),
       Provider = new Security.CookieAuthenticationProvider()
  };
  appBuilder.UseCookieAuthentication(cookieAuthenticationOptions);
  // Now the OpenId authentication
  var notificationHandlers = new OpenIdConnectAuthenticationNotificationHandlers 
  {
     AuthorizationCodeReceived = async(context) =&gt; {
         var jwtSecurityToken = context.JwtSecurityToken;
         // I've written a static method to convert the claims
         // to a user
         var user = await GetOrCreateUser(context.OwinContext, jwtSecurityToken.Claims);
         var signInManager = context.OwinContext.Get&lt;SignInManager&gt;();
         await signInManager.SignInAsync(user, true, false);
     }
  }
  var openIdOptions = new OpenIdConnectAuthenticationOptions
  {
       ClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
       Authority = "https://login.microsoftonline.com/xxxxx.onmicrosoft.com",
       PostLogoutRedirectUri = "https://localhost:52538/Account/Login",
       Notifications = notifcationHandlers
  }
  appBuilder.UseOpenIdConnectAuthentication(openIdOptions);
  </code></pre>
  
  <p>When you click "Active Directory Signin", it posts to "Account/SignInWithOpenId"</p>
  
  <pre><code>public ActionResult SignInWithOpenId()
  {
      // Send an OpenID Connect sign-in request.
      if (!Request.IsAuthenticated)
      {
          var authenticationProperties = new AuthenticationProperties
          {
              RedirectUri = "/"
          };
          HttpContext.GetOwinContext().Authentication.Challenge
          (
              authenticationProperties,
              OpenIdConnectAuthenticationDefaults.AuthenticationType
          );
          return new EmptyResult();
      }
      else
      {
          return RedirectToAction("Index", "Default");
      }
  }
  </code></pre>