InvalidOperationException: sub claim is missing on logout

Smith
Smith
None
2568 Points
74 Posts

I'm getting error on logout in the following line:

var subjectId = HttpContext.User.Identity.
GetSubjectId();
InvalidOperationException: sub claim is missing
IdentityServer4.Extensions .PrincipalExtensions. GetSubjectId(IIdentity identity)
IdentityServer.Controllers. Account.AccountController+<Logout>d__17.MoveNext() in AccountController.cs
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
IdentityServer.Controllers.Account.AccountController+<Logout>d__16.MoveNext() in AccountController.cs
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeActionMethodAsync>d__12.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeNextActionFilterAsync>d__10.MoveNext()
Views: 11939
Total Answered: 1
Total Marked As Answer: 0
Posted On: 21-May-2018 06:06

Share:   fb twitter linkedin
Answers
sam
sam
Member
378 Points
48 Posts
         

It's looking claim are not available for that user. Try to first check if there is any claim available or not. Use following code:

var subjectId = HttpContext.User.Identity.GetAuthenticationMethods().Count() > 0 ? HttpContext.User.Identity.GetSubjectId() : "";
Posted On: 23-May-2018 05:26
 Log In to Chat