Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
83 lines (69 loc) · 2.65 KB
/
Copy pathProgram.cs
File metadata and controls
83 lines (69 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Security.WebAppLib;
using Security.WebAppLib.Example;
using System.Security.Claims;
using System.Security.Principal;
namespace Security.ConsoleApp
{
public class Program
{
static void Main(string[] args)
{
//WinSecurity();
IHost host = CreateHost();
CreateEx(host);
RunEx1(host);
//RunEx2(host);
}
public static void CreateEx(IHost host)
{
var ex1 = host.Services.GetRequiredService<ExploreIdentity>();
var ex2 = host.Services.GetRequiredService<ExploreAuthentication>();
var ex3 = host.Services.GetRequiredService<ExploreAuthorization>();
}
public static IHost CreateHost()
{
HostApplicationBuilder builder = Host.CreateApplicationBuilder([]);
// Configures and registers services for ASP.NET Core Identity,
// authentication, and authorization.
AppService appService = new(builder.Services, builder.Configuration);
appService.AddMyIdentity();
AuthenticationBuilder b = appService.AddMyAuthentication();
b = appService.AddMyAuthenticationJwtBearer(b, "JwtConfig");
//b = appService.AddMyAuthenticationCookie(b);
appService.AddMyAuthorization();
appService.AddMyAuthExamples();
appService.AddMyTestHttpContextAccessor();
IHost app = builder.Build();
return app;
}
public static void RunEx1(IHost host)
{
var ex = host.Services.GetRequiredService<ExploreAuthentication>();
ex.Run().Wait();
}
public static void RunEx2(IHost host)
{
var ex = host.Services.GetRequiredService<ExploreAuthorization>();
ex.Run().Wait();
}
#pragma warning disable CA1416
public static void WinSecurity()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
IIdentity identity1 = identity;
Console.WriteLine($"User: {identity.Name}");
Console.WriteLine($"IsAuthenticated: {identity.IsAuthenticated}");
foreach (var claim in identity.Claims)
{
Console.WriteLine($"Claim Type: {claim.Type}, Value: {claim.Value}");
}
ClaimsPrincipal? principal = ClaimsPrincipal.Current;
principal = WindowsPrincipal.Current;
IPrincipal? principal1 = principal;
}
#pragma warning restore CA1416
}
}