title: ASP.NET MVC app redirects to localhost instead of app URI after Active Directory login
body: <p>I've created a multi tenant ASP.NET MVC project in Visual Studio, and after testing it on <code>localhost</code>, I publishing the app to Azure, went to the Azure portal and changed the app home page uri and response uri to</p>
  
  <pre><code>http:/{appname}.azurewebsites.net
  </code></pre>
  
  <p>while also adding </p>
  
  <pre><code>PostLogoutRedirectUri = "http:/{appname}.azurewebsites.net"
  </code></pre>
  
  <p>in the <code>OpenIdConnectAuthenticationOptions</code> inside <code>Startup.auth.cs</code>.</p>
  
  <p>The problem is, when I try to access the published app, I get redirected to <code>https://localhost:xxxxx</code> after logging in, the same <code>localhost</code> port VS assigned automatically when creating the project. </p>
  
  <p>Is there some other place I need to add the <code>http:/{appname}.azurewebsites.net</code> Uri for my application to redirect to the correct place?</p>
  
  <p>EDIT: In my web.config file:</p>
  
  <pre><code>&lt;configuration&gt;
      &lt;appSettings&gt;
          &lt;add key="webpages:Version" value="3.0.0.0" /&gt;
          &lt;add key="webpages:Enabled" value="false" /&gt;
          &lt;add key="ClientValidationEnabled" value="true" /&gt;
          &lt;add key="UnobtrusiveJavaScriptEnabled" value="true" /&gt;
          &lt;add key="ida:ClientId" value="{Guid}" /&gt;
          &lt;add key="ida:AADInstance" value="https://login.microsoftonline.com/" /&gt;
          &lt;add key="ida:ClientSecret" value="{secret}" /&gt;
          &lt;add key="ida:PostLogoutRedirectUri" value="http://{appname}.azurewebsites.net" /&gt;
      &lt;/appSettings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>In my startup.auth.cs file:
  private static string clientId = </p>
  
  <pre><code>ConfigurationManager.AppSettings["ida:ClientId"];
  private string appKey = ConfigurationManager.AppSettings["ida:ClientSecret"];
  private string resourceEndPoint = "https://manage.office.com";
  private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
  private string authority = aadInstance + "common";
  private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
  
  public void ConfigureAuth(IAppBuilder app)
  {
  
      app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
  
      app.UseCookieAuthentication(new CookieAuthenticationOptions { });
  
      app.UseOpenIdConnectAuthentication(
          new OpenIdConnectAuthenticationOptions
          {
              ClientId = clientId,
              Authority = authority,
              PostLogoutRedirectUri = postLogoutRedirectUri,
              TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
              {
                  ValidateIssuer = false,
              },
              Notifications = new OpenIdConnectAuthenticationNotifications()
              {
                  SecurityTokenValidated = (context) =&gt; 
                  {
                      return Task.FromResult(0);
                  },
                  AuthorizationCodeReceived = (context) =&gt;
                  {
                      var code = context.Code;
  
                      ClientCredential credential = new ClientCredential(clientId, appKey);
                      string tenantID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
                      string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
  
                      AuthenticationContext authContext = new AuthenticationContext(aadInstance + tenantID, new ADALTokenCache(signedInUserID));
                      AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
                      code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, resourceEndPoint);
  
                      return Task.FromResult(0);
                  },
                  AuthenticationFailed = (context) =&gt;
                  {
                      context.OwinContext.Response.Redirect("/Home/Error");
                      context.HandleResponse(); // Suppress the exception
                      return Task.FromResult(0);
                  }
              }
          }
      );
  }
  </code></pre>