Skip to content

Commit c94e4d3

Browse files
committed
add AuthorizationSample project
1 parent 0447f4b commit c94e4d3

File tree

103 files changed

+27705
-58
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+27705
-58
lines changed

.vscode/launch.json

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"name": ".NET Core Launch (CookieSample)",
88
"type": "coreclr",
99
"request": "launch",
10-
"preLaunchTask": "build1",
10+
"preLaunchTask": "BuildCookieSample",
1111
"program": "${workspaceRoot}/src/CookieSample/bin/Debug/netcoreapp2.0/CookieSample.dll",
1212
"args": [],
1313
"cwd": "${workspaceRoot}/src/CookieSample",
@@ -38,7 +38,7 @@
3838
"name": ".NET Core Launch (OAuth)",
3939
"type": "coreclr",
4040
"request": "launch",
41-
"preLaunchTask": "build2",
41+
"preLaunchTask": "BuildOAuthSample",
4242
"program": "${workspaceRoot}/src/OAuthSample/bin/Debug/netcoreapp2.0/OAuthSample.dll",
4343
"args": [],
4444
"cwd": "${workspaceRoot}",
@@ -68,7 +68,7 @@
6868
"name": ".NET Core Launch (OIDCSample)",
6969
"type": "coreclr",
7070
"request": "launch",
71-
"preLaunchTask": "build3",
71+
"preLaunchTask": "BuildOIDCSample",
7272
"program": "${workspaceRoot}/src/OIDCSample/bin/Debug/netcoreapp2.0/OIDCSample.dll",
7373
"args": [],
7474
"cwd": "${workspaceRoot}",
@@ -93,6 +93,36 @@
9393
"sourceFileMap": {
9494
"/Views": "${workspaceRoot}/Views"
9595
}
96+
},
97+
{
98+
"name": ".NET Core Launch (AuthorizationSample)",
99+
"type": "coreclr",
100+
"request": "launch",
101+
"preLaunchTask": "BuildAuthorizationSample",
102+
"program": "${workspaceRoot}/src/AuthorizationSample/bin/Debug/netcoreapp2.0/AuthorizationSample.dll",
103+
"args": [],
104+
"cwd": "${workspaceRoot}",
105+
"stopAtEntry": false,
106+
"launchBrowser": {
107+
"enabled": true,
108+
"args": "${auto-detect-url}",
109+
"windows": {
110+
"command": "cmd.exe",
111+
"args": "/C start ${auto-detect-url}"
112+
},
113+
"osx": {
114+
"command": "open"
115+
},
116+
"linux": {
117+
"command": "xdg-open"
118+
}
119+
},
120+
"env": {
121+
"ASPNETCORE_ENVIRONMENT": "Development"
122+
},
123+
"sourceFileMap": {
124+
"/Views": "${workspaceRoot}/Views"
125+
}
96126
}
97127
]
98128
}

.vscode/tasks.json

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"version": "2.0.0",
3-
"tasks": [
4-
{
5-
"taskName": "build1",
3+
"tasks": [{
4+
"label": "BuildCookieSample",
65
"group": {
76
"kind": "build",
87
"isDefault": true
@@ -18,7 +17,7 @@
1817
]
1918
},
2019
{
21-
"taskName": "build2",
20+
"label": "BuildOAuthSample",
2221
"group": {
2322
"kind": "build",
2423
"isDefault": true
@@ -34,7 +33,7 @@
3433
]
3534
},
3635
{
37-
"taskName": "build3",
36+
"label": "BuildOIDCSample",
3837
"group": {
3938
"kind": "build",
4039
"isDefault": true
@@ -48,6 +47,22 @@
4847
"problemMatcher": [
4948
"$msCompile"
5049
]
50+
},
51+
{
52+
"label": "BuildAuthorizationSample",
53+
"group": {
54+
"kind": "build",
55+
"isDefault": true
56+
},
57+
"type": "shell",
58+
"command": "dotnet",
59+
"args": [
60+
"build",
61+
"${workspaceRoot}/src/AuthorizationSample/AuthorizationSample.csproj"
62+
],
63+
"problemMatcher": [
64+
"$msCompile"
65+
]
5166
}
5267
]
5368
}

AspNetCoreSample.sln

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27009.1
4+
VisualStudioVersion = 15.0.27102.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{95394AEA-085A-4A55-B4CA-3C1C07D9B787}"
77
EndProject
@@ -21,6 +21,18 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Security", "Security", "{22
2121
EndProject
2222
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authentication.JwtBearer", "Security\Microsoft.AspNetCore.Authentication.JwtBearer\Microsoft.AspNetCore.Authentication.JwtBearer.csproj", "{D6129CC4-2333-4467-9F3E-6DD8E65C332B}"
2323
EndProject
24+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{62A46794-5B2A-4A26-AFEC-DC1808ED397D}"
25+
EndProject
26+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Authentication", "Authentication", "{DAB21C84-A7A8-48D7-8EFE-DA1CA716B128}"
27+
EndProject
28+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Authorization", "Authorization", "{BA5F289F-120E-4E94-8E9A-35821E68E5F0}"
29+
EndProject
30+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AuthorizationSample", "src\AuthorizationSample\AuthorizationSample.csproj", "{8C306007-DF69-4943-964C-8110DE004FFA}"
31+
EndProject
32+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authorization", "Security\Microsoft.AspNetCore.Authorization\Microsoft.AspNetCore.Authorization.csproj", "{8C824EF7-2C93-41FE-9E1F-D4C46117B5A6}"
33+
EndProject
34+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authentication", "Security\Microsoft.AspNetCore.Authentication\Microsoft.AspNetCore.Authentication.csproj", "{64AAF232-D5A5-4424-86AD-C1E4BFE92783}"
35+
EndProject
2436
Global
2537
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2638
Debug|Any CPU = Debug|Any CPU
@@ -55,18 +67,35 @@ Global
5567
{D6129CC4-2333-4467-9F3E-6DD8E65C332B}.Debug|Any CPU.Build.0 = Debug|Any CPU
5668
{D6129CC4-2333-4467-9F3E-6DD8E65C332B}.Release|Any CPU.ActiveCfg = Release|Any CPU
5769
{D6129CC4-2333-4467-9F3E-6DD8E65C332B}.Release|Any CPU.Build.0 = Release|Any CPU
70+
{8C306007-DF69-4943-964C-8110DE004FFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
71+
{8C306007-DF69-4943-964C-8110DE004FFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
72+
{8C306007-DF69-4943-964C-8110DE004FFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
73+
{8C306007-DF69-4943-964C-8110DE004FFA}.Release|Any CPU.Build.0 = Release|Any CPU
74+
{8C824EF7-2C93-41FE-9E1F-D4C46117B5A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75+
{8C824EF7-2C93-41FE-9E1F-D4C46117B5A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
76+
{8C824EF7-2C93-41FE-9E1F-D4C46117B5A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
77+
{8C824EF7-2C93-41FE-9E1F-D4C46117B5A6}.Release|Any CPU.Build.0 = Release|Any CPU
78+
{64AAF232-D5A5-4424-86AD-C1E4BFE92783}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
79+
{64AAF232-D5A5-4424-86AD-C1E4BFE92783}.Debug|Any CPU.Build.0 = Debug|Any CPU
80+
{64AAF232-D5A5-4424-86AD-C1E4BFE92783}.Release|Any CPU.ActiveCfg = Release|Any CPU
81+
{64AAF232-D5A5-4424-86AD-C1E4BFE92783}.Release|Any CPU.Build.0 = Release|Any CPU
5882
EndGlobalSection
5983
GlobalSection(SolutionProperties) = preSolution
6084
HideSolutionNode = FALSE
6185
EndGlobalSection
6286
GlobalSection(NestedProjects) = preSolution
63-
{3AC19A59-DDBD-4B2B-B97D-8A287CCC3176} = {95394AEA-085A-4A55-B4CA-3C1C07D9B787}
64-
{B741B4EA-6FAE-427F-8650-376564FBEF96} = {95394AEA-085A-4A55-B4CA-3C1C07D9B787}
65-
{E499FEE6-C9AD-4C2B-AEC1-502A509D4D5E} = {95394AEA-085A-4A55-B4CA-3C1C07D9B787}
66-
{691319AB-BAA4-4454-9FF7-14436F8EEB3C} = {95394AEA-085A-4A55-B4CA-3C1C07D9B787}
67-
{B2DF9019-9658-4EC3-9D67-5FC15F3B29C6} = {95394AEA-085A-4A55-B4CA-3C1C07D9B787}
68-
{65F732C5-5FFA-4032-8114-373FF67D9725} = {95394AEA-085A-4A55-B4CA-3C1C07D9B787}
87+
{3AC19A59-DDBD-4B2B-B97D-8A287CCC3176} = {DAB21C84-A7A8-48D7-8EFE-DA1CA716B128}
88+
{B741B4EA-6FAE-427F-8650-376564FBEF96} = {DAB21C84-A7A8-48D7-8EFE-DA1CA716B128}
89+
{E499FEE6-C9AD-4C2B-AEC1-502A509D4D5E} = {DAB21C84-A7A8-48D7-8EFE-DA1CA716B128}
90+
{691319AB-BAA4-4454-9FF7-14436F8EEB3C} = {DAB21C84-A7A8-48D7-8EFE-DA1CA716B128}
91+
{B2DF9019-9658-4EC3-9D67-5FC15F3B29C6} = {DAB21C84-A7A8-48D7-8EFE-DA1CA716B128}
92+
{65F732C5-5FFA-4032-8114-373FF67D9725} = {62A46794-5B2A-4A26-AFEC-DC1808ED397D}
6993
{D6129CC4-2333-4467-9F3E-6DD8E65C332B} = {229EEA98-E688-47A7-894D-5D17A0B3BC56}
94+
{DAB21C84-A7A8-48D7-8EFE-DA1CA716B128} = {95394AEA-085A-4A55-B4CA-3C1C07D9B787}
95+
{BA5F289F-120E-4E94-8E9A-35821E68E5F0} = {95394AEA-085A-4A55-B4CA-3C1C07D9B787}
96+
{8C306007-DF69-4943-964C-8110DE004FFA} = {BA5F289F-120E-4E94-8E9A-35821E68E5F0}
97+
{8C824EF7-2C93-41FE-9E1F-D4C46117B5A6} = {229EEA98-E688-47A7-894D-5D17A0B3BC56}
98+
{64AAF232-D5A5-4424-86AD-C1E4BFE92783} = {229EEA98-E688-47A7-894D-5D17A0B3BC56}
7099
EndGlobalSection
71100
GlobalSection(ExtensibilityGlobals) = postSolution
72101
SolutionGuid = {8D0A0329-7F31-453F-807D-FED2262A86C4}

Security/Microsoft.AspNetCore.Authentication.JwtBearer/Microsoft.AspNetCore.Authentication.JwtBearer.csproj

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.AspNetCore.Authentication">
10-
<Version>2.0.0</Version>
11-
</PackageReference>
12-
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect">
13-
<Version>2.1.4</Version>
14-
</PackageReference>
9+
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.0.0" />
10+
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="2.1.4" />
1511
</ItemGroup>
1612

1713
</Project>

Security/Microsoft.AspNetCore.Authentication/AuthenticationBuilder.cs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,10 @@ public AuthenticationBuilder(IServiceCollection services)
2525
/// </summary>
2626
public virtual IServiceCollection Services { get; }
2727

28-
/// <summary>
29-
/// Adds a <see cref="AuthenticationScheme"/> which can be used by <see cref="IAuthenticationService"/>.
30-
/// </summary>
31-
/// <typeparam name="TOptions">The <see cref="AuthenticationSchemeOptions"/> type to configure the handler."/>.</typeparam>
32-
/// <typeparam name="THandler">The <see cref="AuthenticationHandler{TOptions}"/> used to handle this scheme.</typeparam>
33-
/// <param name="authenticationScheme">The name of this scheme.</param>
34-
/// <param name="displayName">The display name of this scheme.</param>
35-
/// <param name="configureOptions">Used to configure the scheme options.</param>
36-
/// <returns>The builder.</returns>
37-
public virtual AuthenticationBuilder AddScheme<TOptions, THandler>(string authenticationScheme, string displayName, Action<TOptions> configureOptions)
38-
where TOptions : AuthenticationSchemeOptions, new()
39-
where THandler : AuthenticationHandler<TOptions>
28+
29+
private AuthenticationBuilder AddSchemeHelper<TOptions, THandler>(string authenticationScheme, string displayName, Action<TOptions> configureOptions)
30+
where TOptions : class, new()
31+
where THandler : class, IAuthenticationHandler
4032
{
4133
Services.Configure<AuthenticationOptions>(o =>
4234
{
@@ -53,6 +45,20 @@ public virtual AuthenticationBuilder AddScheme<TOptions, THandler>(string authen
5345
return this;
5446
}
5547

48+
/// <summary>
49+
/// Adds a <see cref="AuthenticationScheme"/> which can be used by <see cref="IAuthenticationService"/>.
50+
/// </summary>
51+
/// <typeparam name="TOptions">The <see cref="AuthenticationSchemeOptions"/> type to configure the handler."/>.</typeparam>
52+
/// <typeparam name="THandler">The <see cref="AuthenticationHandler{TOptions}"/> used to handle this scheme.</typeparam>
53+
/// <param name="authenticationScheme">The name of this scheme.</param>
54+
/// <param name="displayName">The display name of this scheme.</param>
55+
/// <param name="configureOptions">Used to configure the scheme options.</param>
56+
/// <returns>The builder.</returns>
57+
public virtual AuthenticationBuilder AddScheme<TOptions, THandler>(string authenticationScheme, string displayName, Action<TOptions> configureOptions)
58+
where TOptions : AuthenticationSchemeOptions, new()
59+
where THandler : AuthenticationHandler<TOptions>
60+
=> AddSchemeHelper<TOptions, THandler>(authenticationScheme, displayName, configureOptions);
61+
5662
/// <summary>
5763
/// Adds a <see cref="AuthenticationScheme"/> which can be used by <see cref="IAuthenticationService"/>.
5864
/// </summary>
@@ -84,6 +90,17 @@ public virtual AuthenticationBuilder AddRemoteScheme<TOptions, THandler>(string
8490
return AddScheme<TOptions, THandler>(authenticationScheme, displayName, configureOptions: configureOptions);
8591
}
8692

93+
/// <summary>
94+
/// Adds a <see cref="VirtualAuthenticationHandler"/> based authentication handler which can be used to
95+
/// redirect to other authentication schemes.
96+
/// </summary>
97+
/// <param name="authenticationScheme">The name of this scheme.</param>
98+
/// <param name="displayName">The display name of this scheme.</param>
99+
/// <param name="configureOptions">Used to configure the scheme options.</param>
100+
/// <returns>The builder.</returns>
101+
public virtual AuthenticationBuilder AddVirtualScheme(string authenticationScheme, string displayName, Action<VirtualSchemeOptions> configureOptions)
102+
=> AddSchemeHelper<VirtualSchemeOptions, VirtualAuthenticationHandler>(authenticationScheme, displayName, configureOptions);
103+
87104
// Used to ensure that there's always a default sign in scheme that's not itself
88105
private class EnsureSignInScheme<TOptions> : IPostConfigureOptions<TOptions> where TOptions : RemoteAuthenticationOptions
89106
{

Security/Microsoft.AspNetCore.Authentication/AuthenticationHandler.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ namespace Microsoft.AspNetCore.Authentication
2222

2323
protected HttpRequest Request
2424
{
25-
get { return Context.Request; }
25+
get => Context.Request;
2626
}
2727

2828
protected HttpResponse Response
2929
{
30-
get { return Context.Response; }
30+
get => Context.Response;
3131
}
3232

3333
protected PathString OriginalPath => Context.Features.Get<IAuthenticationFeature>()?.OriginalPath ?? Request.Path;
@@ -52,10 +52,7 @@ protected HttpResponse Response
5252

5353
protected string CurrentUri
5454
{
55-
get
56-
{
57-
return Request.Scheme + "://" + Request.Host + Request.PathBase + Request.Path + Request.QueryString;
58-
}
55+
get => Request.Scheme + "://" + Request.Host + Request.PathBase + Request.Path + Request.QueryString;
5956
}
6057

6158
protected AuthenticationHandler(IOptionsMonitor<TOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
@@ -116,15 +113,10 @@ protected virtual async Task InitializeEventsAsync()
116113
/// Called after options/events have been initialized for the handler to finish initializing itself.
117114
/// </summary>
118115
/// <returns>A task</returns>
119-
protected virtual Task InitializeHandlerAsync()
120-
{
121-
return Task.CompletedTask;
122-
}
116+
protected virtual Task InitializeHandlerAsync() => Task.CompletedTask;
123117

124118
protected string BuildRedirectUri(string targetPath)
125-
{
126-
return Request.Scheme + "://" + Request.Host + OriginalPathBase + targetPath;
127-
}
119+
=> Request.Scheme + "://" + Request.Host + OriginalPathBase + targetPath;
128120

129121
public async Task<AuthenticateResult> AuthenticateAsync()
130122
{

Security/Microsoft.AspNetCore.Authentication/LoggingExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static LoggingExtensions()
3939
formatString: "{AuthenticationScheme} was not authenticated. Failure message: {FailureMessage}");
4040
_authSchemeAuthenticated = LoggerMessage.Define<string>(
4141
eventId: 8,
42-
logLevel: LogLevel.Information,
42+
logLevel: LogLevel.Debug,
4343
formatString: "AuthenticationScheme: {AuthenticationScheme} was successfully authenticated.");
4444
_authSchemeNotAuthenticated = LoggerMessage.Define<string>(
4545
eventId: 9,
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.0</TargetFramework>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<VersionPrefix>2.0.2</VersionPrefix>
56
</PropertyGroup>
6-
7+
78
<ItemGroup>
8-
<PackageReference Include="Microsoft.AspNetCore.Authentication.Core" Version="2.0.0" />
9-
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="2.0.0" />
10-
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.0.0" />
11-
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.0.0" />
12-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.0.0" />
13-
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
14-
<PackageReference Include="Microsoft.Extensions.WebEncoders" Version="2.0.0" />
9+
<PackageReference Include="Microsoft.AspNetCore.Authentication.Core" Version="2.1.0-preview1-27569" />
10+
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="2.1.0-preview1-27569" />
11+
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.1.0-preview1-27569" />
12+
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.1.0-preview1-27569" />
13+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.0-preview1-27569" />
14+
<PackageReference Include="Microsoft.Extensions.Options" Version="2.1.0-preview1-27569" />
15+
<PackageReference Include="Microsoft.Extensions.SecurityHelper.Sources" Version="2.1.0-preview1-27569" />
16+
<PackageReference Include="Microsoft.Extensions.WebEncoders" Version="2.1.0-preview1-27569" />
1517
</ItemGroup>
16-
18+
1719
</Project>

0 commit comments

Comments
 (0)