-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.cs
More file actions
234 lines (201 loc) · 9.09 KB
/
Copy pathExample.cs
File metadata and controls
234 lines (201 loc) · 9.09 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Collections.Specialized;
using WK.Integration;
namespace LoginGovPlExample
{
/// <summary>
/// Przykładowa klasa demonstrująca wykorzystanie integracji z login.gov.pl
/// </summary>
public class LoginGovPlExample
{
private const string Issuer = "https://mojausluga.gov.pl";
private const string AssertionConsumerServiceUrl = "https://mojausluga.gov.pl/acs";
private const string ProviderName = "MojaUsługa";
private const bool IsPublicEntity = true; // dla podmiotu publicznego
private const string AuthnLevel = "substantial"; // domyślny poziom uwierzytelnienia
private const string Environment = "int"; // środowisko integracyjne
private readonly LoginGovPlClient _client;
public LoginGovPlExample()
{
// Wczytywanie certyfikatów z magazynu certyfikatów
X509Certificate2 signCertificate = LoadCertificate("CertyfikatDoPodpisu");
X509Certificate2 encryptionCertificate = LoadCertificate("CertyfikatDoSzyfrowania");
// Inicjalizacja klienta
_client = new LoginGovPlClient(
signCertificate,
encryptionCertificate,
Issuer,
AssertionConsumerServiceUrl,
ProviderName,
IsPublicEntity,
AuthnLevel,
Environment
);
}
/// <summary>
/// Metoda rozpoczynająca proces uwierzytelnienia
/// </summary>
/// <returns>Formularz HTML z żądaniem uwierzytelnienia</returns>
public string StartAuthentication()
{
// Generowanie żądania uwierzytelnienia
string requestId = $"ID-{Guid.NewGuid()}";
string authnRequest = _client.GenerateAuthnRequest(requestId);
// Zwrócenie formularza HTML z żądaniem uwierzytelnienia
return authnRequest;
}
/// <summary>
/// Metoda rozpoczynająca proces uwierzytelnienia transgranicznego
/// </summary>
/// <returns>Formularz HTML z żądaniem uwierzytelnienia transgranicznego</returns>
public string StartForeignAuthentication()
{
// Generowanie żądania uwierzytelnienia z włączonym uwierzytelnianiem transgranicznym
string requestId = $"ID-{Guid.NewGuid()}";
string authnRequest = _client.GenerateAuthnRequest(requestId, true);
// Zwrócenie formularza HTML z żądaniem uwierzytelnienia
return authnRequest;
}
/// <summary>
/// Metoda przetwarzająca artefakt SAML zwrócony przez login.gov.pl
/// </summary>
/// <param name="artifact">Artefakt SAML</param>
/// <returns>Informacje o uwierzytelnionym użytkowniku</returns>
public SamlAssertion ProcessArtifact(string artifact)
{
// Wywołanie metody przetwarzającej artefakt
SamlAssertion assertion = _client.ProcessArtifactResponse(artifact);
// Zwrócenie informacji o użytkowniku
return assertion;
}
/// <summary>
/// Metoda wylogowująca użytkownika
/// </summary>
/// <param name="nameId">Identyfikator użytkownika</param>
/// <param name="sessionIndex">Indeks sesji</param>
/// <returns>Status wylogowania</returns>
public SamlLogoutStatus Logout(string nameId, string sessionIndex)
{
// Wywołanie metody wylogowującej
return _client.Logout(nameId, sessionIndex);
}
/// <summary>
/// Metoda pomocnicza do wczytywania certyfikatu z magazynu certyfikatów
/// </summary>
/// <param name="thumbprint">Odcisk palca certyfikatu</param>
/// <returns>Certyfikat</returns>
private X509Certificate2 LoadCertificate(string thumbprint)
{
// W rzeczywistej implementacji certyfikaty mogą być wczytywane z magazynu certyfikatów
// lub z plików
// Przykład wczytywania z magazynu certyfikatów
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
try
{
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates.Find(
X509FindType.FindByThumbprint,
thumbprint,
false
);
if (certs.Count == 0)
{
throw new ApplicationException($"Nie znaleziono certyfikatu o odcisku palca: {thumbprint}");
}
return certs[0];
}
finally
{
store.Close();
}
}
}
/// <summary>
/// Przykładowa klasa kontrolera ASP.NET MVC demonstrująca integrację z login.gov.pl
/// </summary>
public class LoginGovPlController
{
private readonly LoginGovPlExample _loginGovPlExample;
public LoginGovPlController()
{
_loginGovPlExample = new LoginGovPlExample();
}
/// <summary>
/// Akcja rozpoczynająca proces uwierzytelnienia
/// </summary>
public string Login()
{
// Wygenerowanie żądania uwierzytelnienia
string authnRequest = _loginGovPlExample.StartAuthentication();
// Zwrócenie formularza HTML z żądaniem uwierzytelnienia
return authnRequest;
}
/// <summary>
/// Akcja rozpoczynająca proces uwierzytelnienia transgranicznego
/// </summary>
public string LoginForeign()
{
// Wygenerowanie żądania uwierzytelnienia transgranicznego
string authnRequest = _loginGovPlExample.StartForeignAuthentication();
// Zwrócenie formularza HTML z żądaniem uwierzytelnienia
return authnRequest;
}
/// <summary>
/// Akcja przetwarzająca artefakt SAML zwrócony przez login.gov.pl
/// </summary>
public string AssertionConsumerService(string SAMLart)
{
try
{
// Przetworzenie artefaktu SAML
SamlAssertion assertion = _loginGovPlExample.ProcessArtifact(SAMLart);
// Użytkownik został pomyślnie uwierzytelniony
// Tutaj można utworzyć sesję, zapisać dane użytkownika itp.
// Utworzenie ciasteczka sesji
HttpCookie cookie = new HttpCookie("SessionIndex", assertion.SessionIndex);
HttpContext.Current.Response.Cookies.Add(cookie);
// Zapisanie NameID w sesji
HttpContext.Current.Session["NameID"] = assertion.NameID;
// Przekierowanie do strony głównej aplikacji
HttpContext.Current.Response.Redirect("/Home");
return "Użytkownik został pomyślnie uwierzytelniony.";
}
catch (Exception ex)
{
// Obsługa błędów uwierzytelnienia
return $"Błąd uwierzytelnienia: {ex.Message}";
}
}
/// <summary>
/// Akcja wylogowująca użytkownika
/// </summary>
public string Logout()
{
try
{
// Pobranie NameID i SessionIndex z sesji
string nameId = HttpContext.Current.Session["NameID"] as string;
string sessionIndex = HttpContext.Current.Request.Cookies["SessionIndex"]?.Value;
if (string.IsNullOrEmpty(nameId) || string.IsNullOrEmpty(sessionIndex))
{
return "Brak danych sesji do wylogowania.";
}
// Wywołanie metody wylogowującej
SamlLogoutStatus status = _loginGovPlExample.Logout(nameId, sessionIndex);
// Usunięcie danych sesji
HttpContext.Current.Session.Remove("NameID");
HttpContext.Current.Response.Cookies["SessionIndex"].Expires = DateTime.Now.AddDays(-1);
// Przekierowanie do strony logowania
HttpContext.Current.Response.Redirect("/Login");
return $"Użytkownik został wylogowany. Status: {status}";
}
catch (Exception ex)
{
// Obsługa błędów wylogowania
return $"Błąd wylogowania: {ex.Message}";
}
}
}
}