1
1
using Microsoft . AspNetCore . Authorization ;
2
2
using Microsoft . AspNetCore . Mvc ;
3
3
using Microsoft . AspNetCore . Mvc . Filters ;
4
+ using Microsoft . IdentityModel . Protocols . WsTrust ;
4
5
using StrDss . Common ;
6
+ using System . Security . Claims ;
5
7
6
8
namespace StrDss . Api . Authorization
7
9
{
@@ -21,7 +23,8 @@ public void OnAuthorization(AuthorizationFilterContext context)
21
23
_logger = loggerFactory . CreateLogger < StrDssLogger > ( ) ;
22
24
23
25
var user = context . HttpContext . User ;
24
- var username = user ? . Identity ? . Name ?? "Unknown" ;
26
+ var username = user ? . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ?? user ? . FindFirst ( ClaimTypes . Name ) ? . Value ?? "Unknown" ;
27
+
25
28
var ipAddress = context . HttpContext . Connection . RemoteIpAddress ;
26
29
var ip = ipAddress == null ? "Unknown" : ipAddress . ToString ( ) ;
27
30
@@ -39,33 +42,14 @@ public void OnAuthorization(AuthorizationFilterContext context)
39
42
if ( identityProviderNm == "" && clientId != "" )
40
43
{
41
44
identityProviderNm = StrDssIdProviders . Aps ;
45
+ username = clientId ;
42
46
}
43
47
44
48
var displayName = user . GetCustomClaim ( StrDssClaimTypes . DisplayName ) ;
45
49
46
- string userId ;
47
- switch ( identityProviderNm )
48
- {
49
- case StrDssIdProviders . Idir :
50
- userId = user . GetCustomClaim ( StrDssClaimTypes . IdirUserGuid ) ;
51
- break ;
52
- case StrDssIdProviders . BceidBusiness :
53
- userId = user . GetCustomClaim ( StrDssClaimTypes . BceidUserGuid ) ;
54
- break ;
55
- case StrDssIdProviders . StrDss :
56
- userId = user . GetCustomClaim ( StrDssClaimTypes . StrDssUserGuid ) ;
57
- break ;
58
- case StrDssIdProviders . Aps :
59
- userId = clientId ;
60
- break ;
61
- default :
62
- userId = "Unknown" ;
63
- break ;
64
- }
65
-
66
50
if ( _permissions . Length == 0 )
67
51
{
68
- _logger . LogInformation ( $ "[AUTH] User '{ userId } ' is authorized to access { context . ActionDescriptor . DisplayName } from IP address { ip } .") ;
52
+ _logger . LogInformation ( $ "[AUTH] User '{ username } ' is authorized to access { context . ActionDescriptor . DisplayName } from IP address { ip } .") ;
69
53
return ;
70
54
}
71
55
@@ -82,12 +66,12 @@ public void OnAuthorization(AuthorizationFilterContext context)
82
66
83
67
if ( ! hasPermission )
84
68
{
85
- _logger . LogInformation ( $ "[AUTH] User '{ userId } ' does not have permission to access { context . ActionDescriptor . DisplayName } from IP address { ip } .") ;
69
+ _logger . LogInformation ( $ "[AUTH] User '{ username } ' does not have permission to access { context . ActionDescriptor . DisplayName } from IP address { ip } .") ;
86
70
context . Result = new UnauthorizedResult ( ) ; //401
87
71
return ;
88
72
}
89
73
90
- _logger . LogInformation ( $ "[AUTH] User '{ userId } ' is authorized to access { context . ActionDescriptor . DisplayName } from IP address { ip } .") ;
74
+ _logger . LogInformation ( $ "[AUTH] User '{ username } ' is authorized to access { context . ActionDescriptor . DisplayName } from IP address { ip } .") ;
91
75
}
92
76
}
93
77
}
0 commit comments