1
- using Microsoft . Owin ;
2
- using Owin ;
3
- using Microsoft . Owin . Security ;
4
- using Microsoft . Owin . Security . Cookies ;
5
- using Microsoft . Owin . Security . OpenIdConnect ;
6
- using System . Threading . Tasks ;
7
- using Microsoft . IdentityModel . Protocols . OpenIdConnect ;
8
- using System . Configuration ;
9
- using System . Security . Claims ;
10
- using IdentityModel . Client ;
11
- using System ;
1
+ using System ;
12
2
using System . Collections . Generic ;
3
+ using System . Configuration ;
13
4
using System . DirectoryServices . AccountManagement ;
14
- using System . Threading ;
5
+ using System . Security . Claims ;
6
+ using System . Threading . Tasks ;
15
7
using System . Web ;
16
8
using System . Web . Helpers ;
9
+ using IdentityModel . Client ;
17
10
using Lithnet . Laps . Web . App_LocalResources ;
18
11
using Microsoft . IdentityModel . Logging ;
19
- using Microsoft . IdentityModel . Protocols . WsFederation ;
12
+ using Microsoft . IdentityModel . Protocols . OpenIdConnect ;
20
13
using Microsoft . IdentityModel . Tokens ;
21
14
using Microsoft . Owin . Host . SystemWeb ;
15
+ using Microsoft . Owin . Security ;
16
+ using Microsoft . Owin . Security . Cookies ;
22
17
using Microsoft . Owin . Security . Notifications ;
18
+ using Microsoft . Owin . Security . OpenIdConnect ;
23
19
using Microsoft . Owin . Security . WsFederation ;
24
20
using NLog ;
21
+ using Owin ;
25
22
26
23
namespace Lithnet . Laps . Web
27
24
{
28
25
public class Startup
29
26
{
27
+ internal static bool CanLogout = false ;
30
28
internal static string ClaimName { get ; set ; } = "upn" ;
31
29
32
30
internal static IdentityType ClaimType { get ; set ; } = IdentityType . UserPrincipalName ;
@@ -39,12 +37,15 @@ public class Startup
39
37
private readonly string postLogoutRedirectUri = ConfigurationManager . AppSettings [ "oidc:PostLogoutRedirectUri" ] ;
40
38
41
39
private readonly string realm = ConfigurationManager . AppSettings [ "ida:wtrealm" ] ;
40
+ private readonly string signOutWreply = ConfigurationManager . AppSettings [ "ida:signOutWreply" ] ;
42
41
private readonly string metadata = ConfigurationManager . AppSettings [ "ida:metadata" ] ;
43
42
44
43
private static readonly Logger logger = LogManager . GetCurrentClassLogger ( ) ;
45
44
46
45
public void ConfigureOpenIDConnect ( IAppBuilder app )
47
46
{
47
+ Startup . CanLogout = true ;
48
+
48
49
Startup . ClaimName = ConfigurationManager . AppSettings [ "oidc:claimName" ] ?? ClaimTypes . Upn ;
49
50
50
51
if ( Enum . TryParse ( ConfigurationManager . AppSettings [ "oidc:claimType" ] , out IdentityType claimType ) )
@@ -56,7 +57,7 @@ public void ConfigureOpenIDConnect(IAppBuilder app)
56
57
Startup . ClaimType = IdentityType . UserPrincipalName ;
57
58
}
58
59
59
- AntiForgeryConfig . UniqueClaimTypeIdentifier = ConfigurationManager . AppSettings [ "oidc:uniqueClaimTypeIdentifier" ] ?? ClaimTypes . NameIdentifier ;
60
+ AntiForgeryConfig . UniqueClaimTypeIdentifier = ConfigurationManager . AppSettings [ "oidc:uniqueClaimTypeIdentifier" ] ?? ClaimTypes . PrimarySid ;
60
61
61
62
string responseType = ConfigurationManager . AppSettings [ "oidc:responseType" ] ?? OpenIdConnectResponseType . IdToken ;
62
63
@@ -76,10 +77,11 @@ public void ConfigureOpenIDConnect(IAppBuilder app)
76
77
RedirectUri = this . redirectUri ,
77
78
ResponseType = responseType ,
78
79
Scope = OpenIdConnectScope . OpenIdProfile ,
79
- PostLogoutRedirectUri = this . postLogoutRedirectUri ,
80
+ PostLogoutRedirectUri = this . postLogoutRedirectUri ?? new Uri ( new Uri ( this . redirectUri . Trim ( '/' , ' \\ ' ) ) , "Home/LogOut" ) . ToString ( ) ,
80
81
TokenValidationParameters = new TokenValidationParameters
81
82
{
82
- NameClaimType = "name"
83
+ NameClaimType = "name" ,
84
+ SaveSigninToken = true
83
85
} ,
84
86
85
87
Notifications = new OpenIdConnectAuthenticationNotifications
@@ -88,12 +90,6 @@ public void ConfigureOpenIDConnect(IAppBuilder app)
88
90
{
89
91
try
90
92
{
91
- if ( responseType == OpenIdConnectResponseType . IdToken )
92
- {
93
- return ;
94
- }
95
-
96
- // Exchange code for access and ID tokens
97
93
OpenIdConnectConfiguration config = await n . Options . ConfigurationManager . GetConfigurationAsync ( n . Request . CallCancelled ) . ConfigureAwait ( false ) ;
98
94
99
95
TokenClient tokenClient = new TokenClient ( config . TokenEndpoint , this . clientId , this . clientSecret ) ;
@@ -124,7 +120,12 @@ public void ConfigureOpenIDConnect(IAppBuilder app)
124
120
n . Response . Redirect ( $ "/Home/AuthNError?message={ HttpUtility . UrlEncode ( ex . Message ) } ") ;
125
121
}
126
122
} ,
127
- SecurityTokenValidated = Startup . FindClaimIdentityInDirectoryOrFail ,
123
+ SecurityTokenValidated = n =>
124
+ {
125
+ ClaimsIdentity user = n . AuthenticationTicket . Identity ;
126
+ user . AddClaim ( new Claim ( "id_token" , n . ProtocolMessage . IdToken ) ) ;
127
+ return Startup . FindClaimIdentityInDirectoryOrFail ( n ) ;
128
+ } ,
128
129
RedirectToIdentityProvider = n =>
129
130
{
130
131
// If signing out, add the id_token_hint
@@ -148,12 +149,15 @@ public void ConfigureOpenIDConnect(IAppBuilder app)
148
149
149
150
public void ConfigureWindowsAuth ( IAppBuilder app )
150
151
{
152
+ Startup . CanLogout = false ;
153
+ AntiForgeryConfig . UniqueClaimTypeIdentifier = ConfigurationManager . AppSettings [ "ida:uniqueClaimTypeIdentifier" ] ?? ClaimTypes . PrimarySid ;
151
154
ClaimName = ClaimTypes . PrimarySid ;
152
155
ClaimType = IdentityType . Sid ;
153
156
}
154
157
155
158
public void ConfigureWsFederation ( IAppBuilder app )
156
159
{
160
+ Startup . CanLogout = true ;
157
161
Startup . ClaimName = ConfigurationManager . AppSettings [ "ida:claimName" ] ?? ClaimTypes . Upn ;
158
162
159
163
if ( Enum . TryParse ( ConfigurationManager . AppSettings [ "ida:claimType" ] , out IdentityType claimType ) )
@@ -167,7 +171,7 @@ public void ConfigureWsFederation(IAppBuilder app)
167
171
168
172
app . SetDefaultSignInAsAuthenticationType ( CookieAuthenticationDefaults . AuthenticationType ) ;
169
173
170
- AntiForgeryConfig . UniqueClaimTypeIdentifier = ConfigurationManager . AppSettings [ "ida:uniqueClaimTypeIdentifier" ] ?? ClaimTypes . NameIdentifier ;
174
+ AntiForgeryConfig . UniqueClaimTypeIdentifier = ConfigurationManager . AppSettings [ "ida:uniqueClaimTypeIdentifier" ] ?? ClaimTypes . PrimarySid ;
171
175
172
176
app . UseCookieAuthentication ( new CookieAuthenticationOptions
173
177
{
@@ -182,6 +186,7 @@ public void ConfigureWsFederation(IAppBuilder app)
182
186
{
183
187
Wtrealm = this . realm ,
184
188
MetadataAddress = this . metadata ,
189
+ SignOutWreply = this . signOutWreply ?? new Uri ( new Uri ( this . realm . Trim ( '/' , '\\ ' ) ) , "Home/LogOut" ) . ToString ( ) ,
185
190
Notifications = new WsFederationAuthenticationNotifications
186
191
{
187
192
SecurityTokenValidated = Startup . FindClaimIdentityInDirectoryOrFail ,
@@ -193,7 +198,6 @@ public void ConfigureWsFederation(IAppBuilder app)
193
198
private static Task HandleAuthNFailed < TMessage , TOptions > ( AuthenticationFailedNotification < TMessage , TOptions > context )
194
199
{
195
200
Reporting . LogErrorEvent ( EventIDs . OwinAuthNError , LogMessages . AuthNProviderError , context . Exception ) ;
196
-
197
201
context . HandleResponse ( ) ;
198
202
context . Response . Redirect ( $ "/Home/AuthNError?message={ HttpUtility . UrlEncode ( context . Exception ? . Message ?? "Unknown error" ) } ") ;
199
203
0 commit comments