Search Results for

    Show / Hide Table of Contents

    ASP.NET Core SDK

    The Identity Hub has a SDK for ASP.NET Core and ASP.NET Core MVC web sites.

    Download

    Download the SDK from https://www.nuget.org/packages/TheIdentityHub.AspNetCore.Authentication/

    Configuration

    ASP.NET Core Web Site

    .net core 3.0 and higher

    To use the middleware add the following code:

    using TheIdentityHub.AspNetCore.Authentication;
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(auth =>
        {
            auth.DefaultChallengeScheme = TheIdentityHubDefaults.AuthenticationScheme;
            auth.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            auth.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        })
        .AddCookie()
        .AddTheIdentityHubAuthentication(options =>
        {
            options.TheIdentityHubUrl = new Uri("https://www.theidentityhub.com");
            options.Tenant = {tenant};
            options.ClientId = [Client ID] ;
            options.ClientSecret = [Client Secret];
        });
        services.AddMvc(config =>
        {
            var policy = new AuthorizationPolicyBuilder()
            .AddAuthenticationSchemes(TheIdentityHubDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser()
            .Build();
            config.Filters.Add(new AuthorizeFilter(policy));
        })
    }
    public void Configure(IApplicationBuilder app)
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();        
        app.UseAuthentication();
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseEndpoints(endpoints =>
               {
                   endpoints.MapControllers();
                   endpoints.MapRazorPages();
                   endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
               });
        ...
    }
    

    .net core 2.x

    To use the middleware add the following code:

    using TheIdentityHub.AspNetCore.Authentication;
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(auth =>
        {
            auth.DefaultChallengeScheme = TheIdentityHubDefaults.AuthenticationScheme;
            auth.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            auth.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        })
        .AddCookie()
        .AddTheIdentityHubAuthentication(options =>
        {
            options.TheIdentityHubUrl = new Uri("https://www.theidentityhub.com");
            options.Tenant = {tenant};
            options.ClientId = [Client ID] ;
            options.ClientSecret = [Client Secret];
        });
        services.AddMvc(config =>
        {
            var policy = new AuthorizationPolicyBuilder()
            .AddAuthenticationSchemes(TheIdentityHubDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser()
            .Build();
            config.Filters.Add(new AuthorizeFilter(policy));
        })
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();        
        app.UseAuthentication();
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseMvc();
    }
    

    ASP.NET Core Web API Site (Bearer Token Authentication)

    .net core 3.0 and higher

    To use the middleware add the following code:

    using TheIdentityHub.AspNetCore.Authentication;
    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddAuthentication(TheIdentityHubDefaults.AuthenticationScheme).AddTheIdentityHubOAuthBearer(options =>
        {
            options.TheIdentityHubUrl = new Uri("https://www.theidentityhub.com");
            options.Tenant = {tenant};
            options.ClientId = [Client ID] ;
            options.ClientSecret = [Client Secret];
        });    
        ...
    }
    public void Configure(IApplicationBuilder app)
    {
        ...
        app.UseRouting();
        app.UseHsts();    
        app.UseAuthentication();  
        app.UseEndpoints(endpoints => endpoints.MapControllers());
        ...
    }
    

    .net core 2.x

    To use the middleware add the following code:

    using TheIdentityHub.AspNetCore.Authentication;
    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddAuthentication(TheIdentityHubDefaults.AuthenticationScheme).AddTheIdentityHubOAuthBearer(options =>
        {
            options.TheIdentityHubUrl = new Uri("https://www.theidentityhub.com");
            options.Tenant = {tenant};
            options.ClientId = [Client ID] ;
            options.ClientSecret = [Client Secret];
        });    
        ...
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        ...
        app.UseHsts();    
        app.UseAuthentication();  
        app.UseMvc();
        ...
    }
    

    Related

    ASP.NET MVC Core Demo Application
    ASP.NET Core SDK API

    In This Article