Switch to use forwarding instead of virtual handler (#27)

This commit is contained in:
Hao Kung 2018-02-28 14:23:48 -08:00 committed by GitHub
parent b9c006bb59
commit ee6db5a7fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
67 changed files with 109 additions and 21 deletions

View File

@ -18,7 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{15E42EAC-5
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AuthSamples.FunctionalTests", "test\AuthSamples.FunctionalTests\AuthSamples.FunctionalTests.csproj", "{B5C26BE6-655E-4D7F-B756-F286750EF172}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VirtualSchemes.PathSchemeSelection", "samples\VirtualSchemes.PathSchemeSelection\VirtualSchemes.PathSchemeSelection.csproj", "{4E91BD2A-616F-45EE-9647-2F1608D17FB9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PathSchemeSelection", "samples\PathSchemeSelection\PathSchemeSelection.csproj", "{4E91BD2A-616F-45EE-9647-2F1608D17FB9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Identity.ExternalClaims", "samples\Identity.ExternalClaims\Identity.ExternalClaims.csproj", "{D8804E7A-BD7A-4E4B-ACA7-822A37A81B28}"
EndProject

View File

@ -5,7 +5,7 @@ using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
namespace AuthSamples.VirtualScheme.PathSchemeSelection.Controllers
namespace AuthSamples.PathSchemeSelection.Controllers
{
public class AccountController : Controller

View File

@ -1,9 +1,9 @@
using System.Diagnostics;
using AuthSamples.VirtualScheme.PathSchemeSelection.Models;
using AuthSamples.PathSchemeSelection.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace AuthSamples.VirtualScheme.PathSchemeSelection.Controllers
namespace AuthSamples.PathSchemeSelection.Controllers
{
public class HomeController : Controller
{

View File

@ -1,6 +1,6 @@
using System;
namespace AuthSamples.VirtualScheme.PathSchemeSelection.Models
namespace AuthSamples.PathSchemeSelection.Models
{
public class ErrorViewModel
{

View File

@ -8,7 +8,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace AuthSamples.VirtualScheme.PathSchemeSelection
namespace AuthSamples.PathSchemeSelection
{
public class Program
{

View File

@ -1,4 +1,4 @@
AuthSamples.VirtualScheme.PathSchemeSelection
AuthSamples.PathSchemeSelection
=================
Sample demonstrating selecting between cookie and another authentication scheme based on the request:

View File

@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace AuthSamples.PathSchemeSelection
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddScheme<AuthenticationSchemeOptions, ApiAuthHandler>("Api", o => { })
.AddCookie(options =>
{
// Foward any requests that start with /api to that scheme
options.ForwardDefaultSelector = ctx =>
{
return ctx.Request.Path.StartsWithSegments("/api") ? "Api" : null;
};
options.AccessDeniedPath = "/account/denied";
options.LoginPath = "/account/login";
});
}
public class ApiAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
private readonly ClaimsPrincipal _id;
public ApiAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
{
var id = new ClaimsIdentity("Api");
id.AddClaim(new Claim(ClaimTypes.Name, "Hao", ClaimValueTypes.String, "Api"));
_id = new ClaimsPrincipal(id);
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
=> Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(_id, "Api")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
// Must go before UseMvc
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "api",
template: "api/{controller=Home}/{action=Index}/{id?}");
});
}
}
}

View File

@ -0,0 +1,3 @@
@using AuthSamples.PathSchemeSelection
@using AuthSamples.PathSchemeSelection.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

View File

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 31 KiB

View File

Before

Width:  |  Height:  |  Size: 9.5 KiB

After

Width:  |  Height:  |  Size: 9.5 KiB

View File

Before

Width:  |  Height:  |  Size: 8.2 KiB

After

Width:  |  Height:  |  Size: 8.2 KiB

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -29,17 +29,15 @@ namespace AuthSamples.VirtualScheme.PathSchemeSelection
{
services.AddMvc();
services.AddAuthentication("Dynamic")
.AddVirtualScheme("Dynamic", "Dynamic", o =>
{
o.DefaultSelector = ctx =>
{
return ctx.Request.Path.StartsWithSegments("/api") ? "Api" : CookieAuthenticationDefaults.AuthenticationScheme;
};
})
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddScheme<AuthenticationSchemeOptions, ApiAuthHandler>("Api", o => { })
.AddCookie(options =>
{
// Foward any requests that start with /api to that scheme
options.ForwardDefaultSelector = ctx =>
{
return ctx.Request.Path.StartsWithSegments("/api") ? "Api" : null;
};
options.AccessDeniedPath = "/account/denied";
options.LoginPath = "/account/login";
});

View File

@ -1,3 +0,0 @@
@using AuthSamples.VirtualScheme.PathSchemeSelection
@using AuthSamples.VirtualScheme.PathSchemeSelection.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

View File

@ -16,7 +16,7 @@
<ProjectReference Include="..\..\samples\ClaimsTransformation\ClaimsTransformation.csproj" />
<ProjectReference Include="..\..\samples\DynamicSchemes\DynamicSchemes.csproj" />
<ProjectReference Include="..\..\samples\Identity.ExternalClaims\Identity.ExternalClaims.csproj" />
<ProjectReference Include="..\..\samples\VirtualSchemes.PathSchemeSelection\VirtualSchemes.PathSchemeSelection.csproj" />
<ProjectReference Include="..\..\samples\PathSchemeSelection\PathSchemeSelection.csproj" />
<PackageReference Include="Microsoft.AspNetCore" Version="$(MicrosoftAspNetCorePackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="$(MicrosoftAspNetCoreHostingPackageVersion)" />

View File

@ -8,9 +8,9 @@ using Xunit;
namespace AuthSamples.FunctionalTests
{
public class VirtualSchemeTests : IClassFixture<SampleTestFixture<VirtualScheme.PathSchemeSelection.Startup>>
public class PathSchemeSelectionTests : IClassFixture<SampleTestFixture<PathSchemeSelection.Startup>>
{
public VirtualSchemeTests(SampleTestFixture<VirtualScheme.PathSchemeSelection.Startup> fixture)
public PathSchemeSelectionTests(SampleTestFixture<PathSchemeSelection.Startup> fixture)
{
Client = fixture.Client;
}