Click here to Skip to main content
15,116,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all I have the following code where it is working as expected, I would like to make it dynamic so that it should work other tenants too.

C#
public class Startup
{
    // The Client ID is used by the application to uniquely identify itself to Azure AD.
    string clientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"];

    // RedirectUri is the URL where the user will be redirected to after they sign in.
    string redirectUri = System.Configuration.ConfigurationManager.AppSettings["RedirectUri"];

    // Tenant is the tenant ID (e.g. contoso.onmicrosoft.com, or 'common' for multi-tenant)
    static string tenant = System.Configuration.ConfigurationManager.AppSettings["Tenant"];

    // Authority is the URL for authority, composed by Microsoft identity platform endpoint and the tenant name (e.g. https://login.microsoftonline.com/contoso.onmicrosoft.com/v2.0)
    string authority = String.Format(System.Globalization.CultureInfo.InvariantCulture, System.Configuration.ConfigurationManager.AppSettings["Authority"], tenant);

    /// <summary>
    /// Configure OWIN to use OpenIdConnect 
    /// </summary>
    /// <param name="app"></param>
    public void Configuration(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            // Sets the ClientId, authority, RedirectUri as obtained from web.config
            ClientId = clientId,
            Authority = authority,
            RedirectUri = redirectUri,
            // PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it is using the home page
            PostLogoutRedirectUri = redirectUri,
            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
            }
        }
    );
    }

    /// <summary>
    /// Handle failed authentication requests by redirecting the user to the home page with an error in the query string
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context)
    {
        context.HandleResponse();
        context.Response.Redirect("/?errormessage=" + context.Exception.Message);
        return Task.FromResult(0);
    }
}


In my web.config I configured as follows

C#
<appSettings>
  <add key="ClientId" value="" />
  <add key="Tenant" value="" />
  <add key="Authority" value="https://login.microsoftonline.com/{0}/v2.0" />
  <add key="redirectUri " value="https://localhost:44346/Default" />
</appSettings>


Now I would like to move these settings to my database and on my form I have a textbox where user will enter like xyz@demo.com and click on login. When they enter that button I would like to configure the above settings dynamically and navigate to that domain page where they login with their username and password. How can I configure the Configuration to be dynamic

What I have tried:

This setting I need dynamically changed on button click by reading the required setting from database

C#
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            // Sets the ClientId, authority, RedirectUri as obtained from web.config
            ClientId = clientId,
            Authority = authority,
            RedirectUri = redirectUri,
            // PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it is using the home page
            PostLogoutRedirectUri = redirectUri,
            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
            }
        }
    );
Posted
Updated 24-Aug-21 22:46pm
v2

1 solution

The settings for IAppBuilder apply to the entire application, not a specific user. You will need to find another way to achieve your goal.
   
Comments
demouser743 25-Aug-21 9:20am
   
So we can't set it dynamic? What was the other ways to work it out
Richard Deeming 25-Aug-21 9:35am
   
It seems you have an XY problem[^]: you want to do X; you've decided the solution is Y; and now you want us to help you do Y.

Try explaining what you are actually trying to achieve (X), rather than what you are currently trying to do (Y).
demouser743 25-Aug-21 9:57am
   
I have an web application where I am trying to integrate Azure AD, with single tenant I am able to successfully login. We have a requirement where the application can be accessed multiple tenants. So what ever we are setting in web.config, I am configuring the following information ClientId, Tenant in one of the database table. I would like to configure what ever I am configuring currently based on the selected tenant.
Richard Deeming 25-Aug-21 10:03am
   
So you're looking for a multi-tenant login? You should be able to use the approach from this blog post:
The Common Endpoint: Walks Like a Tenant, Talks Like a Tenant… But Is Not a Tenant – CloudIdentity[^]
demouser743 25-Aug-21 10:16am
   
Can I get demo code I am bit confused with the setting there

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900