Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpContext does not contain a definition for SignOutAsync

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 ?

like image 489
AmiNadimi Avatar asked Feb 27 '18 15:02

AmiNadimi


People also ask

What does HttpContext SignOutAsync do?

SignOutAsync(HttpContext, AuthenticationProperties)Sign out a principal for the default authentication scheme. The default scheme for signing out can be configured using DefaultSignOutScheme.

What is HttpContext user?

The User property provides programmatic access to the properties and methods of the IPrincipal interface. Because ASP.NET pages contain a default reference to the System. Web namespace (which contains the HttpContext class), you can reference the members of HttpContext on an .

What is AuthenticateAsync?

AuthenticateAsync(HttpContext) Authenticate the current request using the default authentication scheme. The default authentication scheme can be configured using DefaultAuthenticateScheme. AuthenticateAsync(HttpContext, String) Authenticate the current request using the specified scheme.


2 Answers

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);
like image 186
Igor Avatar answered Jan 26 '23 10:01

Igor


    AuthenticationHttpContextExtensions.SignOutAsync(HttpContext, "Cookies");

    AuthenticationHttpContextExtensions
.SignOutAsync(HttpContext, CookieAuthenticationDefaults.AuthenticationScheme);

try this in a controller

like image 30
Mohamoud Mohamed Avatar answered Jan 26 '23 10:01

Mohamoud Mohamed