-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
I have an issue with windows authentication in a blazor server "system.Objectdisposedexception: Safe handle has been closed". In #12051 @stevensanderson
To resolve this, we need some extra logic that, whenever the circuit connection becomes up (equivalent to OnConnectionUpAsync on a circuit handler), we grab the latest User from the HubContext (or HttpContext if we must, but HubContext is more correct) and pass it to the built-in authentication state provider, which can update its own internal state and call its own NotifyAuthenticationStateChanged.
I dont really understand what it means, and I haven't had luck on finding any sample that shows how it could be solved
Expected Behavior
I expect that It shoulnt drop this system.objectdisposeexception
Steps To Reproduce
I use the following custom authstateprovider:
public class TestAuthStateProvider : AuthenticationStateProvider
{
private readonly IHttpContextAccessor _httpContextAccessor;
public TestAuthStateProvider(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var authuser = GetUser() ?? new ClaimsPrincipal(new ClaimsIdentity());
await Task.Delay(1);
return new AuthenticationState(authuser);
}
public ClaimsPrincipal? GetUser()
{
return _httpContextAccessor?.HttpContext?.User;
}
}
Exceptions (if any)
[01/11/2022 10:49:15 WRN] Unhandled exception rendering component: Safe handle has been closed.
Object name: 'SafeHandle'.
System.ObjectDisposedException: Safe handle has been closed.
Object name: 'SafeHandle'.
at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
at Interop.Advapi32.DuplicateTokenEx(SafeAccessTokenHandle hExistingToken, UInt32 dwDesiredAccess, IntPtr lpTokenAttributes, UInt32 ImpersonationLevel, UInt32 TokenType, SafeAccessTokenHandle& phNewToken)
at System.Security.Principal.WindowsPrincipal.IsInRole(SecurityIdentifier sid)
at System.Security.Principal.WindowsPrincipal.IsInRole(String role)
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
at Microsoft.AspNetCore.Authorization.Infrastructure.RolesAuthorizationRequirement.HandleRequirementAsync(AuthorizationHandlerContext context, RolesAuthorizationRequirement requirement)
at Microsoft.AspNetCore.Authorization.AuthorizationHandler`1.HandleAsync(AuthorizationHandlerContext context)
at Microsoft.AspNetCore.Authorization.Infrastructure.PassThroughAuthorizationHandler.HandleAsync(AuthorizationHandlerContext context)
at Microsoft.AspNetCore.Authorization.DefaultAuthorizationService.AuthorizeAsync(ClaimsPrincipal user, Object resource, IEnumerable`1 requirements)
at Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.IsAuthorizedAsync(ClaimsPrincipal user)
at Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.OnParametersSetAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
[01/11/2022 10:49:16 ERR] Unhandled exception in circuit 'iu_JUNYyhrCtLJxkwbzxX87ohvDjr3seIuRUs3BmtCg'.
System.ObjectDisposedException: Safe handle has been closed.
Object name: 'SafeHandle'.
at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
at Interop.Advapi32.DuplicateTokenEx(SafeAccessTokenHandle hExistingToken, UInt32 dwDesiredAccess, IntPtr lpTokenAttributes, UInt32 ImpersonationLevel, UInt32 TokenType, SafeAccessTokenHandle& phNewToken)
at System.Security.Principal.WindowsPrincipal.IsInRole(SecurityIdentifier sid)
at System.Security.Principal.WindowsPrincipal.IsInRole(String role)
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
at Microsoft.AspNetCore.Authorization.Infrastructure.RolesAuthorizationRequirement.HandleRequirementAsync(AuthorizationHandlerContext context, RolesAuthorizationRequirement requirement)
at Microsoft.AspNetCore.Authorization.AuthorizationHandler`1.HandleAsync(AuthorizationHandlerContext context)
at Microsoft.AspNetCore.Authorization.Infrastructure.PassThroughAuthorizationHandler.HandleAsync(AuthorizationHandlerContext context)
at Microsoft.AspNetCore.Authorization.DefaultAuthorizationService.AuthorizeAsync(ClaimsPrincipal user, Object resource, IEnumerable`1 requirements)
at Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.IsAuthorizedAsync(ClaimsPrincipal user)
at Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.OnParametersSetAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
[01/11/2022 10:49:16 WRN] Unhandled exception rendering component: Safe handle has been closed.
Object name: 'SafeHandle'.
System.ObjectDisposedException: Safe handle has been closed.
Object name: 'SafeHandle'.
at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
at Interop.Advapi32.DuplicateTokenEx(SafeAccessTokenHandle hExistingToken, UInt32 dwDesiredAccess, IntPtr lpTokenAttributes, UInt32 ImpersonationLevel, UInt32 TokenType, SafeAccessTokenHandle& phNewToken)
at System.Security.Principal.WindowsPrincipal.IsInRole(SecurityIdentifier sid)
at System.Security.Principal.WindowsPrincipal.IsInRole(String role)
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
at Microsoft.AspNetCore.Authorization.Infrastructure.RolesAuthorizationRequirement.HandleRequirementAsync(AuthorizationHandlerContext context, RolesAuthorizationRequirement requirement)
at Microsoft.AspNetCore.Authorization.AuthorizationHandler`1.HandleAsync(AuthorizationHandlerContext context)
at Microsoft.AspNetCore.Authorization.Infrastructure.PassThroughAuthorizationHandler.HandleAsync(AuthorizationHandlerContext context)
at Microsoft.AspNetCore.Authorization.DefaultAuthorizationService.AuthorizeAsync(ClaimsPrincipal user, Object resource, IEnumerable`1 requirements)
at Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.IsAuthorizedAsync(ClaimsPrincipal user)
at Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.OnParametersSetAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
[01/11/2022 10:49:16 ERR] Unhandled exception in circuit 'iu_JUNYyhrCtLJxkwbzxX87ohvDjr3seIuRUs3BmtCg'.
System.ObjectDisposedException: Safe handle has been closed.
Object name: 'SafeHandle'.
.NET Version
blazor server template .net 6
Anything else?
No response