HttpContext does not contain a definition for SignOutAsync
Solution 1
HttpContext
should be a reference to a field in your Controller
and that you are not referring to a/the type HttpContext
. If that is not the case then that is the cause of your problem, change your code to use the field/variable and not the type.
So if the field name is httpContext
then use that as calling an extension methods is done by referring to the method on an instance and not a type as the instance is also passed in as the first parameter of the extension method.
await httpContext.SignInAsync(IdentityConstants.ApplicationScheme);
Solution 2
AuthenticationHttpContextExtensions.SignOutAsync(HttpContext, "Cookies");
AuthenticationHttpContextExtensions
.SignOutAsync(HttpContext, CookieAuthenticationDefaults.AuthenticationScheme);
try this in a controller
Solution 3
insted of
await HttpContext.SignInAsync(IdentityConstants.ApplicationScheme);
should use
await httpContext.SignInAsync(IdentityConstants.ApplicationScheme,ClaimsPrincipal.Current);
or could use
await Microsoft.AspNetCore.Authentication.AuthenticationHttpContextExtensions.SignInAsync(IdentityConstants.ApplicationScheme,ClaimsPrincipal.Current);
AmiNadimi
Web and Handset developer (History) Currently working as a DevOps Specialist. Kind of curious about everything in programming world... Caring about planet earth
Updated on June 14, 2022Comments
-
AmiNadimi almost 2 years
I'm trying to write a custom authentication manager that will handle user login and logout and many other stuff in my ASP .Net Core 2.x app, but i'm stuck in the first place.
i have tried the way suggested in this Microsoft Article but when i try to implement Sign-in, it shows the
HttpContext does not contain a definition for SignOutAsync
error. i have all the references as suggested in the article :public async void SignIn(HttpContext httpContext, UserDbModel user, bool isPersistent = false) { ClaimsIdentity identity = new ClaimsIdentity(this.GetUserClaims(user), CookieAuthenticationDefaults.AuthenticationScheme); ClaimsPrincipal principal = new ClaimsPrincipal(identity); await HttpContext.SignInAsync(IdentityConstants.ApplicationScheme); }
below code works but it is obsolete :
await HttpContext.Authentication.SignOutAsync(...)
references in class :
using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Authentication;
What's missing here ? maybe these extension-methods are removed in new versions, if so ... how do i implement the authentication in it's correct way ?