1
- using Microsoft . AspNetCore . Authentication ;
2
- using Microsoft . AspNetCore . Authentication . Cookies ;
1
+ using Microsoft . AspNetCore . Authentication . Cookies ;
3
2
using Microsoft . AspNetCore . Http ;
4
3
using Microsoft . Extensions . Options ;
5
4
using System . Security . Claims ;
@@ -26,6 +25,11 @@ public BackofficeUserAccessor(
26
25
_httpContextAccessor = httpContextAccessor ;
27
26
}
28
27
28
+
29
+ /// <summary>
30
+ /// Updated to use ChunkingCookieManager as per Sean Maloney's answer on our.umbraco.com
31
+ /// https://our.umbraco.com/forum/umbraco-9/106857-how-do-i-determine-if-a-backoffice-user-is-logged-in-from-a-razor-view#comment-341847
32
+ /// </summary>
29
33
public ClaimsIdentity BackofficeUser
30
34
{
31
35
get
@@ -35,16 +39,17 @@ public ClaimsIdentity BackofficeUser
35
39
if ( httpContext == null )
36
40
return new ClaimsIdentity ( ) ;
37
41
38
- CookieAuthenticationOptions cookieOptions = _cookieOptionsSnapshot . Get ( global ::Umbraco . Cms . Core . Constants . Security . BackOfficeAuthenticationType ) ;
39
- string ? backOfficeCookie = httpContext . Request . Cookies [ cookieOptions . Cookie . Name ! ] ;
42
+ var cookieOptions = _cookieOptionsSnapshot . Get ( global ::Umbraco . Cms . Core . Constants . Security . BackOfficeAuthenticationType ) ;
43
+ var cookieManager = new ChunkingCookieManager ( ) ;
44
+ var backOfficeCookie = cookieManager . GetRequestCookie ( httpContext , cookieOptions . Cookie . Name ! ) ;
40
45
41
46
if ( string . IsNullOrEmpty ( backOfficeCookie ) )
42
47
return new ClaimsIdentity ( ) ;
43
48
44
- AuthenticationTicket ? unprotected = cookieOptions . TicketDataFormat . Unprotect ( backOfficeCookie ! ) ;
45
- ClaimsIdentity backOfficeIdentity = unprotected ! . Principal . GetUmbracoIdentity ( ) ;
49
+ var unprotected = cookieOptions . TicketDataFormat . Unprotect ( backOfficeCookie ! ) ;
50
+ var backOfficeIdentity = unprotected ! . Principal . GetUmbracoIdentity ( ) ;
46
51
47
- return backOfficeIdentity ;
52
+ return backOfficeIdentity ?? new ClaimsIdentity ( ) ;
48
53
}
49
54
}
50
55
}
0 commit comments