I made this and change and it is working as expected.
public void Configuration(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
SetApp(app);
}
private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context)
{
context.HandleResponse();
context.Response.Redirect("/?errormessage=" + context.Exception.Message);
return Task.FromResult(0);
}
private IAppBuilder SetApp(IAppBuilder app)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["azureSetting"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT DomainName, ClientId, TenantId, RedirectUrl, AuthorityUrl FROM AzureMapping", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions(row["DomainName"].ToString())
{
// Sets the ClientId, authority, RedirectUri as obtained from web.config
ClientId = row["ClientId"].ToString(),
Authority = string.Format(System.Globalization.CultureInfo.InvariantCulture, row["AuthorityUrl"].ToString(), row["TenantId"].ToString()),
RedirectUri = row["RedirectUrl"].ToString(),
// PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it is using the home page
PostLogoutRedirectUri = "https://localhost:44346/signout-callback-oidc",
Scope = OpenIdConnectScope.OpenIdProfile,
// ResponseType is set to request the code id_token - which contains basic information about the signed-in user
ResponseType = OpenIdConnectResponseType.CodeIdToken,
// OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to OnAuthenticationFailed method
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = OnAuthenticationFailed
}
}
);
}
}
}
}
}
return app;
}
On login.aspx page I am checking for the particular domain and calling the oprion
protected void btnSignIn_Click(object sender, EventArgs e)
{
if (!Request.IsAuthenticated)
{
HttpContext.Current.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties { RedirectUri = "/" },
new MailAddress(txtEmail.Text).Host);
Session["Domain"] = new MailAddress(txtEmail.Text).Host;
}
}