Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion NuGetPackageVerifier.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
"adx-nonshipping": {
"rules": [],
"packages": {
"Microsoft.AspNetCore.ChunkingCookieManager.Sources": {}
"Microsoft.AspNetCore.ChunkingCookieManager.Sources": {},
"Microsoft.AspNetCore.Authentication.WsFederation": {
"Exclusions": {
"THIRDPARTY_DEPENDENCY_NOT_REGISTERED": {
"Microsoft.AspNetCore.Authentication.WsFederation; .NETStandard,Version=v2.0": "New dependency for out of band release."
}
}
}
}
},
"Default": {
Expand Down
59 changes: 58 additions & 1 deletion Security.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26621.2
VisualStudioVersion = 15.0.26730.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4D2B6A51-2F9F-44F5-8131-EA5CAC053652}"
EndProject
Expand Down Expand Up @@ -65,6 +65,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authorization.Policy", "src\Microsoft.AspNetCore.Authorization.Policy\Microsoft.AspNetCore.Authorization.Policy.csproj", "{58194599-F07D-47A3-9DF2-E21A22C5EF9E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authentication.WsFederation", "src\Microsoft.AspNetCore.Authentication.WsFederation\Microsoft.AspNetCore.Authentication.WsFederation.csproj", "{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WsFedSample", "samples\WsFedSample\WsFedSample.csproj", "{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authentication.WsFederation.Test", "test\Microsoft.AspNetCore.Authentication.WsFederation.Test\Microsoft.AspNetCore.Authentication.WsFederation.Test.csproj", "{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -455,6 +461,54 @@ Global
{58194599-F07D-47A3-9DF2-E21A22C5EF9E}.Release|x64.Build.0 = Release|Any CPU
{58194599-F07D-47A3-9DF2-E21A22C5EF9E}.Release|x86.ActiveCfg = Release|Any CPU
{58194599-F07D-47A3-9DF2-E21A22C5EF9E}.Release|x86.Build.0 = Release|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Debug|x64.ActiveCfg = Debug|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Debug|x64.Build.0 = Debug|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Debug|x86.ActiveCfg = Debug|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Debug|x86.Build.0 = Debug|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Release|Any CPU.Build.0 = Release|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Release|x64.ActiveCfg = Release|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Release|x64.Build.0 = Release|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Release|x86.ActiveCfg = Release|Any CPU
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB}.Release|x86.Build.0 = Release|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Debug|x64.ActiveCfg = Debug|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Debug|x64.Build.0 = Debug|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Debug|x86.ActiveCfg = Debug|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Debug|x86.Build.0 = Debug|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Release|Any CPU.Build.0 = Release|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Release|x64.ActiveCfg = Release|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Release|x64.Build.0 = Release|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Release|x86.ActiveCfg = Release|Any CPU
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6}.Release|x86.Build.0 = Release|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Debug|x64.ActiveCfg = Debug|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Debug|x64.Build.0 = Debug|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Debug|x86.ActiveCfg = Debug|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Debug|x86.Build.0 = Debug|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Release|Any CPU.Build.0 = Release|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Release|x64.ActiveCfg = Release|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Release|x64.Build.0 = Release|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Release|x86.ActiveCfg = Release|Any CPU
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -484,6 +538,9 @@ Global
{3A7AD414-EBDE-4F92-B307-4E8F19B6117E} = {F8C0AA27-F3FB-4286-8E4C-47EF86B539FF}
{51563775-C659-4907-9BAF-9995BAB87D01} = {7BF11F3A-60B6-4796-B504-579C67FFBA34}
{58194599-F07D-47A3-9DF2-E21A22C5EF9E} = {4D2B6A51-2F9F-44F5-8131-EA5CAC053652}
{DCD17A1A-2A4A-4AE2-AA0B-11F45C425AAB} = {4D2B6A51-2F9F-44F5-8131-EA5CAC053652}
{4C47C517-A3DD-4D84-B7F5-6C4B224FC1D6} = {F8C0AA27-F3FB-4286-8E4C-47EF86B539FF}
{92D88010-9BF1-45E5-B103-5F98ECA6F2D1} = {7BF11F3A-60B6-4796-B504-579C67FFBA34}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {ABF8089E-43D0-4010-84A7-7A9DCFE49357}
Expand Down
64 changes: 64 additions & 0 deletions samples/WsFedSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;

namespace WsFedSample
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.ConfigureLogging(factory =>
{
factory.AddConsole();
factory.AddDebug();
factory.AddFilter("Console", level => level >= LogLevel.Information);
factory.AddFilter("Debug", level => level >= LogLevel.Information);
})
.UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, 44307, listenOptions =>
{
// Configure SSL
var serverCertificate = LoadCertificate();
listenOptions.UseHttps(serverCertificate);
});
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();

host.Run();
}

private static X509Certificate2 LoadCertificate()
{
var assembly = typeof(Startup).GetTypeInfo().Assembly;
var embeddedFileProvider = new EmbeddedFileProvider(assembly, "WsFedSample");
var certificateFileInfo = embeddedFileProvider.GetFileInfo("compiler/resources/cert.pfx");
using (var certificateStream = certificateFileInfo.CreateReadStream())
{
byte[] certificatePayload;
using (var memoryStream = new MemoryStream())
{
certificateStream.CopyTo(memoryStream);
certificatePayload = memoryStream.ToArray();
}

return new X509Certificate2(certificatePayload, "testPassword");
}
}
}
}
28 changes: 28 additions & 0 deletions samples/WsFedSample/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "https://localhost:44307/",
"sslPort": 44318
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "https://localhost:44307/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WsFedSample": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:44307/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
168 changes: 168 additions & 0 deletions samples/WsFedSample/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.WsFederation;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace WsFedSample
{
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.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: doesn't DefaultSignInScheme fallback to DefaultScheme?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not correctly in the 2.0.0 branch we're targeting. That was just fixed in 2.1 and 2.0.x.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, yeah that's too bad

sharedOptions.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme;
})
.AddWsFederation(options =>
{
options.Wtrealm = "https://Tratcheroutlook.onmicrosoft.com/WsFedSample";
options.MetadataAddress = "https://login.windows.net/cdc690f9-b6b8-4023-813a-bae7143d1f87/FederationMetadata/2007-06/FederationMetadata.xml";
// options.CallbackPath = "/";
// options.SkipUnrecognizedRequests = true;
})
.AddCookie();
}

public void Configure(IApplicationBuilder app)
{
app.UseDeveloperExceptionPage();
app.UseAuthentication();

app.Run(async context =>
{
if (context.Request.Path.Equals("/signedout"))
{
await WriteHtmlAsync(context.Response, async res =>
{
await res.WriteAsync($"<h1>You have been signed out.</h1>");
await res.WriteAsync("<a class=\"btn btn-link\" href=\"/\">Sign In</a>");
});
return;
}

if (context.Request.Path.Equals("/signout"))
{
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just use the empty overload since you are setting the defaults?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could. This just mirrors the template code.

await WriteHtmlAsync(context.Response, async res =>
{
await context.Response.WriteAsync($"<h1>Signed out {HtmlEncode(context.User.Identity.Name)}</h1>");
await context.Response.WriteAsync("<a class=\"btn btn-link\" href=\"/\">Sign In</a>");
});
return;
}

if (context.Request.Path.Equals("/signout-remote"))
{
// Redirects
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await context.SignOutAsync(WsFederationDefaults.AuthenticationScheme, new AuthenticationProperties()
{
RedirectUri = "/signedout"
});
return;
}

if (context.Request.Path.Equals("/Account/AccessDenied"))
{
await WriteHtmlAsync(context.Response, async res =>
{
await context.Response.WriteAsync($"<h1>Access Denied for user {HtmlEncode(context.User.Identity.Name)} to resource '{HtmlEncode(context.Request.Query["ReturnUrl"])}'</h1>");
await context.Response.WriteAsync("<a class=\"btn btn-link\" href=\"/signout\">Sign Out</a>");
});
return;
}

// DefaultAuthenticateScheme causes User to be set
var user = context.User;

// This is what [Authorize] calls
// var user = await context.AuthenticateAsync();

// This is what [Authorize(ActiveAuthenticationSchemes = WsFederationDefaults.AuthenticationScheme)] calls
// var user = await context.AuthenticateAsync(WsFederationDefaults.AuthenticationScheme);

// Not authenticated
if (user == null || !user.Identities.Any(identity => identity.IsAuthenticated))
{
// This is what [Authorize] calls
await context.ChallengeAsync();

// This is what [Authorize(ActiveAuthenticationSchemes = WsFederationDefaults.AuthenticationScheme)] calls
// await context.ChallengeAsync(WsFederationDefaults.AuthenticationScheme);

return;
}

// Authenticated, but not authorized
if (context.Request.Path.Equals("/restricted") && !user.Identities.Any(identity => identity.HasClaim("special", "true")))
{
await context.ForbidAsync();
return;
}

await WriteHtmlAsync(context.Response, async response =>
{
await response.WriteAsync($"<h1>Hello Authenticated User {HtmlEncode(user.Identity.Name)}</h1>");
await response.WriteAsync("<a class=\"btn btn-default\" href=\"/restricted\">Restricted</a>");
await response.WriteAsync("<a class=\"btn btn-default\" href=\"/signout\">Sign Out</a>");
await response.WriteAsync("<a class=\"btn btn-default\" href=\"/signout-remote\">Sign Out Remote</a>");

await response.WriteAsync("<h2>Claims:</h2>");
await WriteTableHeader(response, new string[] { "Claim Type", "Value" }, context.User.Claims.Select(c => new string[] { c.Type, c.Value }));
});
});
}

private static async Task WriteHtmlAsync(HttpResponse response, Func<HttpResponse, Task> writeContent)
{
var bootstrap = "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\" integrity=\"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u\" crossorigin=\"anonymous\">";

response.ContentType = "text/html";
await response.WriteAsync($"<html><head>{bootstrap}</head><body><div class=\"container\">");
await writeContent(response);
await response.WriteAsync("</div></body></html>");
}

private static async Task WriteTableHeader(HttpResponse response, IEnumerable<string> columns, IEnumerable<IEnumerable<string>> data)
{
await response.WriteAsync("<table class=\"table table-condensed\">");
await response.WriteAsync("<tr>");
foreach (var column in columns)
{
await response.WriteAsync($"<th>{HtmlEncode(column)}</th>");
}
await response.WriteAsync("</tr>");
foreach (var row in data)
{
await response.WriteAsync("<tr>");
foreach (var column in row)
{
await response.WriteAsync($"<td>{HtmlEncode(column)}</td>");
}
await response.WriteAsync("</tr>");
}
await response.WriteAsync("</table>");
}

private static string HtmlEncode(string content) =>
string.IsNullOrEmpty(content) ? string.Empty : HtmlEncoder.Default.Encode(content);
}
}
27 changes: 27 additions & 0 deletions samples/WsFedSample/WsFedSample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Authentication.WsFederation\Microsoft.AspNetCore.Authentication.WsFederation.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.0.0" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to cause issues. You should always use a ProjectReference to anything that is built from the same repo.

cc @JunTaoLuo

Copy link
Contributor

@JunTaoLuo JunTaoLuo Sep 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see why this fails universe. We will build this for this release without using Universe as I'll build locally instead. The reason we wanted to take this approach is because we wanted to use the release 2.0.0 packages as dependencies.

<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="compiler\resources\cert.pfx" />
</ItemGroup>

</Project>
Binary file added samples/WsFedSample/compiler/resources/cert.pfx
Binary file not shown.
Loading