Skip to content

Commit 41588a8

Browse files
committed
More auth updates
1 parent 75c11b0 commit 41588a8

24 files changed

Lines changed: 400 additions & 162 deletions

IdentityManager2.sln

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27520.0
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29519.181
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{21035206-B373-4994-901B-2C9E882B5852}"
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdentityManager2", "src\IdentityManager2\IdentityManager2.csproj", "{E4718661-6384-4378-82C3-56D66F6E060F}"
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Host", "src\Host\Host.csproj", "{D024CAF4-4371-45C5-9728-D06B462CD4B3}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HostWithLocalAuth", "src\HostWithLocalAuth\HostWithLocalAuth.csproj", "{20FEF10D-D003-48A7-A931-4DEB80796E08}"
13+
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hosts.Shared", "src\Hosts.Shared\Hosts.Shared.csproj", "{BE7A2243-B7D0-4D32-92F6-0DD4C8DA8208}"
15+
EndProject
1216
Global
1317
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1418
Debug|Any CPU = Debug|Any CPU
@@ -23,13 +27,23 @@ Global
2327
{D024CAF4-4371-45C5-9728-D06B462CD4B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
2428
{D024CAF4-4371-45C5-9728-D06B462CD4B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
2529
{D024CAF4-4371-45C5-9728-D06B462CD4B3}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{20FEF10D-D003-48A7-A931-4DEB80796E08}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{20FEF10D-D003-48A7-A931-4DEB80796E08}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{20FEF10D-D003-48A7-A931-4DEB80796E08}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{20FEF10D-D003-48A7-A931-4DEB80796E08}.Release|Any CPU.Build.0 = Release|Any CPU
34+
{BE7A2243-B7D0-4D32-92F6-0DD4C8DA8208}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{BE7A2243-B7D0-4D32-92F6-0DD4C8DA8208}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{BE7A2243-B7D0-4D32-92F6-0DD4C8DA8208}.Release|Any CPU.ActiveCfg = Release|Any CPU
37+
{BE7A2243-B7D0-4D32-92F6-0DD4C8DA8208}.Release|Any CPU.Build.0 = Release|Any CPU
2638
EndGlobalSection
2739
GlobalSection(SolutionProperties) = preSolution
2840
HideSolutionNode = FALSE
2941
EndGlobalSection
3042
GlobalSection(NestedProjects) = preSolution
3143
{E4718661-6384-4378-82C3-56D66F6E060F} = {21035206-B373-4994-901B-2C9E882B5852}
3244
{D024CAF4-4371-45C5-9728-D06B462CD4B3} = {21035206-B373-4994-901B-2C9E882B5852}
45+
{20FEF10D-D003-48A7-A931-4DEB80796E08} = {21035206-B373-4994-901B-2C9E882B5852}
46+
{BE7A2243-B7D0-4D32-92F6-0DD4C8DA8208} = {21035206-B373-4994-901B-2C9E882B5852}
3347
EndGlobalSection
3448
GlobalSection(ExtensibilityGlobals) = postSolution
3549
SolutionGuid = {22A3DD5E-832A-4FFC-B0B9-1A3D07313154}

src/Host/Host.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<!--<PackageReference Include="IdentityServer4" Version="2.4.0" />-->
9-
</ItemGroup>
10-
11-
<ItemGroup>
8+
<ProjectReference Include="..\Hosts.Shared\Hosts.Shared.csproj" />
129
<ProjectReference Include="..\IdentityManager2\IdentityManager2.csproj" />
1310
</ItemGroup>
1411
</Project>

src/Host/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.Extensions.DependencyInjection;
33
using System;
4-
using Host.InMemory;
4+
using Hosts.Shared.InMemory;
55
using IdentityManager2.Assets;
66
using Microsoft.AspNetCore.Http;
77
using Microsoft.Extensions.FileProviders;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Security.Claims;
5+
using System.Threading.Tasks;
6+
using Hosts.Shared.InMemory;
7+
using IdentityManager2;
8+
using Microsoft.AspNetCore.Authentication;
9+
using Microsoft.AspNetCore.Mvc;
10+
11+
namespace HostWithLocalAuth
12+
{
13+
public class LoginController : Controller
14+
{
15+
private readonly ICollection<InMemoryUser> users;
16+
17+
public LoginController(ICollection<InMemoryUser> users)
18+
{
19+
this.users = users ?? throw new ArgumentNullException(nameof(users));
20+
}
21+
22+
[HttpGet("login")]
23+
public IActionResult Login(string returnUrl)
24+
{
25+
return View(new LoginModel {ReturnUrl = returnUrl});
26+
}
27+
28+
[HttpPost("login")]
29+
[ValidateAntiForgeryToken]
30+
public async Task<IActionResult> Login(LoginModel model)
31+
{
32+
var user = users.FirstOrDefault(x => x.Username == model.Username && x.Password == model.Password);
33+
34+
if (user != null)
35+
{
36+
var claims = new List<Claim>
37+
{
38+
new Claim("sub", user.Subject),
39+
new Claim("name", user.Username)
40+
};
41+
42+
foreach (var role in user.Claims.Where(x => x.Type == IdentityManagerConstants.ClaimTypes.Role))
43+
{
44+
claims.Add(new Claim(IdentityManagerConstants.ClaimTypes.Role, role.Value));
45+
}
46+
47+
await HttpContext.SignInAsync("cookie", new ClaimsPrincipal(new ClaimsIdentity(claims, "cookie")));
48+
49+
if (Url.IsLocalUrl(model.ReturnUrl)) return LocalRedirect(model.ReturnUrl);
50+
return LocalRedirect("~/");
51+
}
52+
53+
ModelState.AddModelError("", "Invalid username or password");
54+
return View(model);
55+
}
56+
}
57+
58+
public class LoginModel
59+
{
60+
public string Username { get; set; }
61+
public string Password { get; set; }
62+
public string ReturnUrl { get; set; }
63+
}
64+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\Hosts.Shared\Hosts.Shared.csproj" />
9+
<ProjectReference Include="..\IdentityManager2\IdentityManager2.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

src/HostWithLocalAuth/Program.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace HostWithLocalAuth
5+
{
6+
public class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
CreateHostBuilder(args).Build().Run();
11+
}
12+
13+
public static IHostBuilder CreateHostBuilder(string[] args) =>
14+
Host.CreateDefaultBuilder(args)
15+
.ConfigureWebHostDefaults(webBuilder =>
16+
{
17+
webBuilder.UseStartup<Startup>();
18+
});
19+
}
20+
}
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:60759",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"HostWithLocalAuth": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"applicationUrl": "http://localhost:5000",
22+
"environmentVariables": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
}
25+
}
26+
}
27+
}

src/HostWithLocalAuth/Startup.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using Hosts.Shared.InMemory;
3+
using IdentityManager2.Assets;
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.FileProviders;
8+
9+
namespace HostWithLocalAuth
10+
{
11+
public class Startup
12+
{
13+
public void ConfigureServices(IServiceCollection services)
14+
{
15+
// In-memory IdentityManagerService (demo only)
16+
services.AddIdentityManager(options =>
17+
{
18+
options.SecurityConfiguration.HostAuthenticationType = "cookie";
19+
options.SecurityConfiguration.HostChallengeType = "cookie";
20+
})
21+
.AddIdentityMangerService<InMemoryIdentityManagerService>();
22+
23+
var rand = new Random();
24+
services.AddSingleton(x => Users.Get(rand.Next(5000, 20000)));
25+
services.AddSingleton(x => Roles.Get(rand.Next(15)));
26+
27+
services.AddAuthentication("cookie")
28+
.AddCookie("cookie", options =>
29+
{
30+
options.LoginPath = "/login";
31+
});
32+
}
33+
34+
public void Configure(IApplicationBuilder app)
35+
{
36+
app.UseDeveloperExceptionPage();
37+
38+
app.UseRouting();
39+
40+
app.UseStaticFiles();
41+
42+
app.UseAuthentication();
43+
app.UseAuthorization();
44+
45+
app.UseFileServer(new FileServerOptions
46+
{
47+
RequestPath = new PathString("/assets"),
48+
FileProvider = new EmbeddedFileProvider(typeof(EmbeddedHtmlResult).Assembly, "IdentityManager2.Assets")
49+
});
50+
51+
app.UseEndpoints(x =>
52+
{
53+
//x.MapIdentityManager("/idm");
54+
x.MapDefaultControllerRoute();
55+
});
56+
}
57+
}
58+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@model HostWithLocalAuth.LoginModel
2+
3+
@{
4+
Layout = null;
5+
}
6+
7+
<!DOCTYPE html>
8+
9+
<html>
10+
<head>
11+
<title>title</title>
12+
</head>
13+
<body>
14+
<div>
15+
<form asp-action="Login" method="post">
16+
<input asp-for="ReturnUrl" type="hidden"/>
17+
<h2>Username</h2>
18+
<input asp-for="Username"/>
19+
<h2>Password</h2>
20+
<input asp-for="Password"/>
21+
<button type="submit">Submit</button>
22+
</form>
23+
</div>
24+
</body>
25+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

0 commit comments

Comments
 (0)