Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

[Prototype] Add multitenant version of dynamic schemes sample #44

Closed
wants to merge 8 commits into from
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
17 changes: 16 additions & 1 deletion AuthSamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DynamicSchemes", "samples\D
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomPolicyProvider", "samples\CustomPolicyProvider\CustomPolicyProvider.csproj", "{70299871-8FF5-4521-AD56-48BB6E07BA13}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StaticFilesAuth", "samples\StaticFilesAuth\StaticFilesAuth.csproj", "{0F013930-E66F-4F8B-95BE-CDFB417ACE3E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StaticFilesAuth", "samples\StaticFilesAuth\StaticFilesAuth.csproj", "{0F013930-E66F-4F8B-95BE-CDFB417ACE3E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Options.MultiTenant", "samples\Options.MultiTenant\Options.MultiTenant.csproj", "{4C01214B-F7EC-4518-B1DF-5400E6D41D8E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -126,6 +128,18 @@ Global
{0F013930-E66F-4F8B-95BE-CDFB417ACE3E}.Release|x64.Build.0 = Release|Any CPU
{0F013930-E66F-4F8B-95BE-CDFB417ACE3E}.Release|x86.ActiveCfg = Release|Any CPU
{0F013930-E66F-4F8B-95BE-CDFB417ACE3E}.Release|x86.Build.0 = Release|Any CPU
{4C01214B-F7EC-4518-B1DF-5400E6D41D8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C01214B-F7EC-4518-B1DF-5400E6D41D8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C01214B-F7EC-4518-B1DF-5400E6D41D8E}.Debug|x64.ActiveCfg = Debug|Any CPU
{4C01214B-F7EC-4518-B1DF-5400E6D41D8E}.Debug|x64.Build.0 = Debug|Any CPU
{4C01214B-F7EC-4518-B1DF-5400E6D41D8E}.Debug|x86.ActiveCfg = Debug|Any CPU
{4C01214B-F7EC-4518-B1DF-5400E6D41D8E}.Debug|x86.Build.0 = Debug|Any CPU
{4C01214B-F7EC-4518-B1DF-5400E6D41D8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C01214B-F7EC-4518-B1DF-5400E6D41D8E}.Release|Any CPU.Build.0 = Release|Any CPU
{4C01214B-F7EC-4518-B1DF-5400E6D41D8E}.Release|x64.ActiveCfg = Release|Any CPU
{4C01214B-F7EC-4518-B1DF-5400E6D41D8E}.Release|x64.Build.0 = Release|Any CPU
{4C01214B-F7EC-4518-B1DF-5400E6D41D8E}.Release|x86.ActiveCfg = Release|Any CPU
{4C01214B-F7EC-4518-B1DF-5400E6D41D8E}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -139,6 +153,7 @@ Global
{F2F7A64C-870C-40C9-B5FC-F8952F1572B3} = {CA4538F5-9DA8-4139-B891-A13279889F79}
{70299871-8FF5-4521-AD56-48BB6E07BA13} = {CA4538F5-9DA8-4139-B891-A13279889F79}
{0F013930-E66F-4F8B-95BE-CDFB417ACE3E} = {CA4538F5-9DA8-4139-B891-A13279889F79}
{4C01214B-F7EC-4518-B1DF-5400E6D41D8E} = {CA4538F5-9DA8-4139-B891-A13279889F79}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {39E3AF62-B1FD-4156-92AA-F4FA99B5AD89}
Expand Down
2 changes: 1 addition & 1 deletion samples/DynamicSchemes/DynamicSchemes.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.2;net461</TargetFrameworks>
<TargetFrameworks>netcoreapp2.2</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="$(MicrosoftAspNetCorePackageVersion)" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.2;net461</TargetFrameworks>
<TargetFrameworks>netcoreapp2.2</TargetFrameworks>
<UserSecretsId>aspnet-Identity.ExternalClaims-E95BE154-CB1B-4633-A2E0-B2DF12FE8BD3</UserSecretsId>
</PropertyGroup>
<ItemGroup>
Expand Down
38 changes: 38 additions & 0 deletions samples/Options.MultiTenant/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;

namespace AuthSamples.Options.MultiTenant.Controllers
{
public class AuthController : Controller
{
private readonly IAuthenticationSchemeProvider _schemeProvider;
private readonly TenantOptionsCache _cache;
private readonly TenantResolver _resolver;

public AuthController(IAuthenticationSchemeProvider schemeProvider, TenantOptionsCache optionsCache, TenantResolver resolver)
{
_schemeProvider = schemeProvider;
_cache = optionsCache;
_resolver = resolver;
}

public IActionResult Remove(string scheme)
{
_schemeProvider.RemoveScheme(scheme);
_cache.Remove(scheme);
return Redirect($"/?tenant={_resolver.ResolveTenant()}");
}

[HttpPost]
public async Task<IActionResult> AddOrUpdate(string scheme, string clientId, string clientSecret)
{
if (await _schemeProvider.GetSchemeAsync(scheme) == null)
{
_schemeProvider.AddScheme(new AuthenticationScheme(scheme, scheme, typeof(SimpleAuthHandler)));
}
_cache.Update(scheme, new SimpleOptions { ClientId = clientId, ClientSecret = clientSecret });
return Redirect($"/?tenant={_resolver.ResolveTenant()}");
}
}
}
23 changes: 23 additions & 0 deletions samples/Options.MultiTenant/Options.MultiTenant.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.2</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="$(MicrosoftAspNetCorePackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="$(MicrosoftAspNetCoreAuthenticationGooglePackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="$(MicrosoftAspNetCoreAuthorizationPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Extensions" Version="$(MicrosoftAspNetCoreDataProtectionExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="$(MicrosoftAspNetCoreDiagnosticsPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="$(MicrosoftAspNetCoreDiagnosticsEntityFrameworkCorePackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="$(MicrosoftAspNetCoreHostingPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="$(MicrosoftAspNetCoreMvcPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="$(MicrosoftAspNetCoreStaticFilesPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="$(MicrosoftAspNetCoreServerIISIntegrationPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="$(MicrosoftAspNetCoreServerKestrelPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="$(MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="$(MicrosoftExtensionsConfigurationUserSecretsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsLoggingConsolePackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="$(MicrosoftExtensionsLoggingDebugPackageVersion)" />
<PackageReference Include="Microsoft.NET.Sdk.Razor" Version="$(MicrosoftNETSdkRazorPackageVersion)" PrivateAssets="All" />
</ItemGroup>
</Project>
9 changes: 9 additions & 0 deletions samples/Options.MultiTenant/Pages/About.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@page
@model AboutModel
@{
ViewData["Title"] = "About";
}
<h2>@ViewData["Title"]</h2>
<h3>@Model.Message</h3>

<p>Use this area to provide additional information.</p>
18 changes: 18 additions & 0 deletions samples/Options.MultiTenant/Pages/About.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Options.MultiTenant.Pages
{
public class AboutModel : PageModel
{
public string Message { get; set; }

public void OnGet()
{
Message = "Your application description page.";
}
}
}
19 changes: 19 additions & 0 deletions samples/Options.MultiTenant/Pages/Contact.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@page
@model ContactModel
@{
ViewData["Title"] = "Contact";
}
<h2>@ViewData["Title"]</h2>
<h3>@Model.Message</h3>

<address>
One Microsoft Way<br />
Redmond, WA 98052-6399<br />
<abbr title="Phone">P:</abbr>
425.555.0100
</address>

<address>
<strong>Support:</strong> <a href="mailto:Support@example.com">Support@example.com</a><br />
<strong>Marketing:</strong> <a href="mailto:Marketing@example.com">Marketing@example.com</a>
</address>
18 changes: 18 additions & 0 deletions samples/Options.MultiTenant/Pages/Contact.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Options.MultiTenant.Pages
{
public class ContactModel : PageModel
{
public string Message { get; set; }

public void OnGet()
{
Message = "Your contact page.";
}
}
}
23 changes: 23 additions & 0 deletions samples/Options.MultiTenant/Pages/Error.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@page
@model ErrorModel
@{
ViewData["Title"] = "Error";
}

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
</p>
<p>
<strong>Development environment should not be enabled in deployed applications</strong>, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>, and restarting the application.
</p>
21 changes: 21 additions & 0 deletions samples/Options.MultiTenant/Pages/Error.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Options.MultiTenant.Pages
{
public class ErrorModel : PageModel
{
public string RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
}
}
53 changes: 53 additions & 0 deletions samples/Options.MultiTenant/Pages/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
@page
@using Microsoft.AspNetCore.Authentication
@using Microsoft.Extensions.Options
@inject IAuthenticationSchemeProvider SchemeProvider
@inject IOptionsMonitor<SimpleOptions> Options
@inject TenantResolver Tenants
@model IndexModel
@{
ViewData["Title"] = "Home page";
}

<div class="row">
<h2>Current Authentication Schemes for Tenant: @Tenants.ResolveTenant()</h2>
<ul>
@{
var schemes = await SchemeProvider.GetAllSchemesAsync();
foreach (var scheme in schemes)
{
<li><b>Scheme:</b> @scheme.Name -- <b>Client Id:Secret=</b> {@Options.Get(scheme.Name).ClientId}:{@Options.Get(scheme.Name).ClientSecret} <a asp-area="" asp-controller="Auth" asp-action="Remove" asp-route-scheme="@scheme.Name" asp-route-tenant="@Tenants.ResolveTenant()">Remove</a></li>
}
}
</ul>
</div>
<div class="row">
<h2>Add or update a scheme:</h2>
<form asp-controller="Auth" asp-action="AddOrUpdate" asp-route-tenant="@Tenants.ResolveTenant()" method="post" class="form-horizontal" role="form">
<div class="form-group">
<label class="col-md-2 control-label">Scheme</label>
<div class="col-md-10">
<input type="text" name="scheme" />
</div>
</div>

<div class="form-group">
<label class="col-md-2 control-label">ClientId</label>
<div class="col-md-10">
<input type="text" name="ClientId" />
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">ClientSecret</label>
<div class="col-md-10">
<input type="text" name="ClientSecret" />
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="submit" class="btn btn-default">Add/Update</button>
</div>
</div>
</form>
</div>
17 changes: 17 additions & 0 deletions samples/Options.MultiTenant/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Options.MultiTenant.Pages
{
public class IndexModel : PageModel
{
public void OnGet()
{

}
}
}
71 changes: 71 additions & 0 deletions samples/Options.MultiTenant/Pages/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - Options.MultiTenant</title>

<environment include="Development">
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="~/css/site.css" />
</environment>
<environment exclude="Development">
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
</environment>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a asp-page="/Index" class="navbar-brand">Options.MultiTenant</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a asp-page="/Index">Home</a></li>
<li><a asp-page="/About">About</a></li>
<li><a asp-page="/Contact">Contact</a></li>
</ul>
</div>
</div>
</nav>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>&copy; 2017 - Options.MultiTenant</p>
</footer>
</div>

<environment include="Development">
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
</environment>
<environment exclude="Development">
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.2.0.min.js"
asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
asp-fallback-test="window.jQuery"
crossorigin="anonymous"
integrity="sha384-K+ctZQ+LL8q6tP7I94W+qzQsfRV2a+AfHIi9k8z8l9ggpc8X+Ytst4yBo/hH+8Fk">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/bootstrap.min.js"
asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.min.js"
asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal"
crossorigin="anonymous"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa">
</script>
<script src="~/js/site.min.js" asp-append-version="true"></script>
</environment>

@RenderSection("Scripts", required: false)
</body>
</html>
18 changes: 18 additions & 0 deletions samples/Options.MultiTenant/Pages/_ValidationScriptsPartial.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<environment include="Development">
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
</environment>
<environment exclude="Development">
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"
asp-fallback-src="~/lib/jquery-validation/dist/jquery.validate.min.js"
asp-fallback-test="window.jQuery && window.jQuery.validator"
crossorigin="anonymous"
integrity="sha384-Fnqn3nxp3506LP/7Y3j/25BlWeA3PXTyT1l78LjECcPaKCV12TsZP7yyMxOe/G/k">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validation.unobtrusive/3.2.6/jquery.validate.unobtrusive.min.js"
asp-fallback-src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"
asp-fallback-test="window.jQuery && window.jQuery.validator && window.jQuery.validator.unobtrusive"
crossorigin="anonymous"
integrity="sha384-JrXK+k53HACyavUKOsL+NkmSesD2P+73eDMrbTtTk0h4RmOF8hF8apPlkp26JlyH">
</script>
</environment>
3 changes: 3 additions & 0 deletions samples/Options.MultiTenant/Pages/_ViewImports.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@using AuthSamples.Options.MultiTenant
@namespace Options.MultiTenant.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Loading