Skip to content

Commit b9e0f1b

Browse files
committed
initial check-in
1 parent 2728586 commit b9e0f1b

File tree

84 files changed

+40885
-17
lines changed

Some content is hidden

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

84 files changed

+40885
-17
lines changed

.gitignore

Lines changed: 401 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<AssemblyName>AWS.Samples.Amazon.Cognito.Demo</AssemblyName>
6+
<RootNamespace>AWS.Samples.Amazon.Cognito.Demo</RootNamespace>
7+
</PropertyGroup>
8+
9+
<PropertyGroup Condition=" '$(RunConfiguration)' == 'aspnetcorewebapp' " />
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="3.1.4" />
12+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.2" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30104.148
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AWS.Samples.Amazon.Cognito.Authentication.Demo", "AWS.Samples.Amazon.Cognito.Authentication.Demo.csproj", "{25071CFE-7AA1-442C-A0F0-CE23260093F0}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{25071CFE-7AA1-442C-A0F0-CE23260093F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{25071CFE-7AA1-442C-A0F0-CE23260093F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{25071CFE-7AA1-442C-A0F0-CE23260093F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{25071CFE-7AA1-442C-A0F0-CE23260093F0}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {A2A8CC0D-2FAC-4B3B-B2FF-552F3CE33AFF}
24+
EndGlobalSection
25+
EndGlobal

Controllers/Admin2Controller.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: MIT-0
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
6+
* software and associated documentation files (the "Software"), to deal in the Software
7+
* without restriction, including without limitation the rights to use, copy, modify,
8+
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so.
10+
*
11+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
12+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
13+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
14+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
15+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
16+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
*/
18+
19+
using Microsoft.AspNetCore.Authorization;
20+
using Microsoft.AspNetCore.Mvc;
21+
22+
namespace AWS.Samples.Amazon.Cognito.Demo.Controllers
23+
{
24+
//Role Based Authorization: look under startup.cs for further instruction on how to enable this and how it works
25+
public class Admin2Controller : Controller
26+
{
27+
28+
//This enforces a policy requiring the logged in user to be part of the Cognito group Admin.
29+
//To use this on a Page/Controller/Route, refer to the policy name in the Authorize attribute:
30+
//[Authorize(Roles = "Admin")]
31+
32+
33+
[Authorize(Roles = "Admin")]
34+
[HttpGet]
35+
public IActionResult Index()
36+
{
37+
return View();
38+
}
39+
}
40+
}

Controllers/AdminController.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: MIT-0
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
6+
* software and associated documentation files (the "Software"), to deal in the Software
7+
* without restriction, including without limitation the rights to use, copy, modify,
8+
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so.
10+
*
11+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
12+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
13+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
14+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
15+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
16+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
*/
18+
19+
using Microsoft.AspNetCore.Authorization;
20+
using Microsoft.AspNetCore.Mvc;
21+
22+
namespace AWS.Samples.Amazon.Cognito.Demo.Controllers
23+
{
24+
25+
//Policy based authorization
26+
public class AdminController : Controller
27+
{
28+
29+
//This enforces a policy requiring the logged in user to be part of the Cognito group Admin.
30+
//To use this on a Page/Controller/Route, refer to the policy name in the Authorize attribute:
31+
//[Authorize(Roles = "Admin")]
32+
33+
34+
[Authorize(Policy = "AdminOnly")]
35+
[HttpGet]
36+
public IActionResult Index()
37+
{
38+
return View();
39+
}
40+
}
41+
}

Controllers/HomeController.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: MIT-0
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
6+
* software and associated documentation files (the "Software"), to deal in the Software
7+
* without restriction, including without limitation the rights to use, copy, modify,
8+
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so.
10+
*
11+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
12+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
13+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
14+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
15+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
16+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
*/
18+
19+
using AWS.Samples.Amazon.Cognito.Demo.Models;
20+
using Microsoft.AspNetCore.Mvc;
21+
using Microsoft.Extensions.Logging;
22+
using System.Diagnostics;
23+
24+
namespace AWS.Samples.Amazon.Cognito.Demo.Controllers
25+
{
26+
public class HomeController : Controller
27+
{
28+
private readonly ILogger<HomeController> _logger;
29+
30+
public HomeController(ILogger<HomeController> logger)
31+
{
32+
_logger = logger;
33+
}
34+
35+
public IActionResult Index()
36+
{
37+
return View();
38+
}
39+
40+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
41+
public IActionResult Error()
42+
{
43+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
44+
}
45+
}
46+
}

Controllers/ProductsController.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.AspNetCore.Authentication.Cookies;
3+
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
4+
using Microsoft.AspNetCore.Authorization;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
/*
8+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
9+
* SPDX-License-Identifier: MIT-0
10+
*
11+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
12+
* software and associated documentation files (the "Software"), to deal in the Software
13+
* without restriction, including without limitation the rights to use, copy, modify,
14+
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
15+
* permit persons to whom the Software is furnished to do so.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
namespace AWS.Samples.Amazon.Cognito.Demo.Controllers
26+
{
27+
public class ProductsController : Controller
28+
{
29+
// GET: /<controller>/
30+
[Authorize]
31+
[HttpGet]
32+
public IActionResult Index()
33+
{
34+
return View();
35+
}
36+
}
37+
38+
public class ExternalAuthenticationController : Controller
39+
{
40+
public IActionResult CallBack()
41+
{
42+
//caputure the user object
43+
return RedirectToAction("Index", "Products");
44+
}
45+
46+
public IActionResult SignOut()
47+
{
48+
var callbackUrl = Url.Page("/", pageHandler: null, values: null, protocol: Request.Scheme);
49+
return SignOut(
50+
new AuthenticationProperties { RedirectUri = callbackUrl },
51+
CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme
52+
);
53+
}
54+
}
55+
}

Models/ErrorViewModel.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: MIT-0
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
6+
* software and associated documentation files (the "Software"), to deal in the Software
7+
* without restriction, including without limitation the rights to use, copy, modify,
8+
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so.
10+
*
11+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
12+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
13+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
14+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
15+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
16+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
*/
18+
19+
namespace AWS.Samples.Amazon.Cognito.Demo.Models
20+
{
21+
public class ErrorViewModel
22+
{
23+
public string RequestId { get; set; }
24+
25+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
26+
}
27+
}

Program.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: MIT-0
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
6+
* software and associated documentation files (the "Software"), to deal in the Software
7+
* without restriction, including without limitation the rights to use, copy, modify,
8+
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so.
10+
*
11+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
12+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
13+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
14+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
15+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
16+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
*/
18+
19+
using Microsoft.AspNetCore.Hosting;
20+
using Microsoft.Extensions.Configuration;
21+
using Microsoft.Extensions.Hosting;
22+
23+
namespace AWS.Samples.Amazon.Cognito.Demo
24+
{
25+
public class Program
26+
{
27+
public static void Main(string[] args)
28+
{
29+
CreateHostBuilder(args).Build().Run();
30+
}
31+
32+
public static IHostBuilder CreateHostBuilder(string[] args) =>
33+
Host.CreateDefaultBuilder(args)
34+
.ConfigureWebHostDefaults(webBuilder =>
35+
{
36+
webBuilder.UseStartup<Startup>();
37+
})
38+
.ConfigureAppConfiguration((hostingContext, config) =>
39+
{
40+
config.AddJsonFile(
41+
"appsettings.local.json",
42+
optional: true,
43+
reloadOnChange: true);
44+
});
45+
}
46+
}

Properties/launchSettings.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:44312",
7+
"sslPort": 44314
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"aspnetcorewebapp": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"applicationUrl": "https://localhost:44314;http://localhost:5000",
22+
"environmentVariables": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)