title: Passing parameters to Azure Active Directory authentication
body: <p>I have an ASP.Net MVC Application, Owin, and I'm using Azure Active Directory authentication as well.</p>
  
  <p>I want to pass a parameter when the user is redirected to the Azure AD authentication page. So when the user signs in or signs up I want to pass <code>ProjectId (int)</code> as a parameter.</p>
  
  <p>After the user signs in/up and is redirected to my Application I want to receive the <code>ProjectId</code> I passed as a parameter. </p>
  
  <p>How can I achieve that?</p>
  
  <p>edit: Adding code</p>
  
  <pre><code>// The ACR claim is used to indicate which policy was executed
  public const string AcrClaimType = "http://schemas.microsoft.com/claims/authnclassreference";
  public const string PolicyKey = "b2cpolicy";
  private const string OidcMetadataSuffix = "/.well-known/openid-configuration";
  
  public void ConfigureAuth(IAppBuilder app)
  {
      app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
  
      app.UseCookieAuthentication(new CookieAuthenticationOptions());
  
      OpenIdConnectAuthenticationOptions options = new OpenIdConnectAuthenticationOptions
      {
          // These are standard OpenID Connect parameters, with values pulled from web.config
          ClientId = ClientId,
          RedirectUri = RedirectUri,
          PostLogoutRedirectUri = RedirectUri,
          UseTokenLifetime = false,
          Notifications = new OpenIdConnectAuthenticationNotifications
          {
              AuthenticationFailed = AuthenticationFailed,
              RedirectToIdentityProvider = OnRedirectToIdentityProvider,
              SecurityTokenValidated = OnSecurityTokenValidated
          },
          Scope = "openid",
          ResponseType = "id_token",
  
          // The PolicyConfigurationManager takes care of getting the correct Azure AD authentication
          // endpoints from the OpenID Connect metadata endpoint.  It is included in the PolicyAuthHelpers folder.
          ConfigurationManager = new PolicyConfigurationManager(
              string.Format(CultureInfo.InvariantCulture, AadInstance, Tenant, "/v2.0", OidcMetadataSuffix),
              new[] { SignUpPolicyId, SignInPolicyId, ProfilePolicyId }),
  
          // This piece is optional - it is used for displaying the user's name in the navigation bar.
          TokenValidationParameters = new TokenValidationParameters
          {
              NameClaimType = "name"
          }
      };
  
      app.UseOpenIdConnectAuthentication(options);
  }
  
  private Task OnRedirectToIdentityProvider(
          RedirectToIdentityProviderNotification&lt;OpenIdConnectMessage, OpenIdConnectAuthenticationOptions&gt; notification)
  {
      if (notification.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest)
      {
          var currentPolicy =
              notification.OwinContext.Authentication.AuthenticationResponseRevoke.AuthenticationTypes
                  .FirstOrDefault(x =&gt; x.StartsWith("b2c"));
          notification.ProtocolMessage.IssuerAddress = notification.ProtocolMessage.IssuerAddress.Split('?')[0];
          notification.ProtocolMessage.Parameters.Add("p", currentPolicy);
      }
      else
      {
          **// The value right now for the state is sort of "hijacked" and assigned by Microsoft**
          //notification.ProtocolMessage.Parameters["state"] = "OpenIdConnect.AuthenticationProperties=sRt-teBcxsd239viWo...... ";
  
          var currentPolicy = notification.OwinContext.Authentication.AuthenticationResponseChallenge.Properties
              .Dictionary[PolicyKey];
          notification.ProtocolMessage.IssuerAddress = notification.ProtocolMessage.IssuerAddress.Split('?')[0];
          notification.ProtocolMessage.Parameters.Add("p", currentPolicy);
      }
  
      return Task.FromResult(0);
  }
  
  private async Task OnSecurityTokenValidated(SecurityTokenValidatedNotification&lt;OpenIdConnectMessage, OpenIdConnectAuthenticationOptions&gt; notification)
  {
      await MyClass.CreatePrincipal(notification.AuthenticationTicket.Identity);
  }
  
  private Task AuthenticationFailed(
              AuthenticationFailedNotification&lt;OpenIdConnectMessage, OpenIdConnectAuthenticationOptions&gt; notification)
  {
      notification.HandleResponse();
      notification.Response.Redirect("/Home/Error?message=" + notification.Exception.Message);
      return Task.FromResult(0);
  }
  </code></pre>