User.Identity.IsAuthenticated always false in .net core custom authentication

Pankaj Rawat

Can anyone please check below code and let me know why I'm getting always false (User.Identity.IsAuthenticated)??. I'm getting cookie on my browser properly and able to get value from Claim but "User.Identity.IsAuthenticated" always false.

public async Task<IActionResult> Login(string phoneNumber, int otp, string returnUrl)
    {
        if (this.accountService.ValidateOTP(phoneNumber, otp))
        {
            var claims = new List<Claim>
            {
                new Claim(ClaimTypes.MobilePhone, phoneNumber),
                new Claim(ClaimTypes.Name, phoneNumber)
            };
            var userIdentity = new ClaimsIdentity();
            userIdentity.AddClaims(claim);
            ClaimsPrincipal userPrincipal = new ClaimsPrincipal(userIdentity);

            await HttpContext.Authentication.SignOutAsync("MyCookieMiddlewareInstance");
            await HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance", userPrincipal,
                new AuthenticationProperties
                {
                    ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
                    IsPersistent = false,
                    AllowRefresh = false
                });

            if (string.IsNullOrWhiteSpace(returnUrl))
            {
                return RedirectToAction("Create", "Ad");
            }
            else
            {
                return Redirect(returnUrl);
            }
        }

        return BadRequest();
    }

enter image description here

Kévin Chalet

ClaimsIdentity.IsAuthenticated returns false when ClaimsIdentity.AuthenticationType is null or empty. To avoid that, stop using the parameterless ClaimsIdentity constructor and use the overload accepting an authenticationType parameter:

var userIdentity = new ClaimsIdentity("Custom");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

User.Identity.IsAuthenticated always return false .NET CORE C#

Jwt Bearer Authentication middleware always sets User.Identity.IsAuthenticated to false

OWIN authentication succeeds but User.Identity.IsAuthenticated is false

CustomRequestCultureProvider: context.User.Identity.IsAuthenticated is always false

User.Identity.IsAuthenticated always false after PasswordSignInAsync gives success

HttpContext.Current.User.Identity.IsAuthenticated = false always in Nginx Enviroment

User.Identity.IsAuthenticated is false in a non-auth methods after successful login in asp.net core

IsAuthenticated is always false in Custom Authorization Attribute (.NET Core 2.2 and Jason Web Token)

.net Core how to use User.Identity.IsAuthenticated

User.Identity.IsAuthenticated AND _signInManager.IsSignedIn(User) return always null / fasle IN MVC CORE 6 2022

ASP.NET Core Identity 2: User.IsInRole always returns false

Using Client Credentials flow on identityserver4 and custom AuthorizationHandler User.Identity.isAuthenticated = false

User.Identity.IsAuthenticated = false after login successfully mvc core 3.0 with web api

.net identity custom authentication

.Net Core Custom Authentication using API Keys with Identity Server 4

User.Identity.IsAuthenticated is false after log in and setting cookies

SignInManager.PasswordSignInAsync() succeeds, but User.Identity.IsAuthenticated is false

User.Identity.IsAuthenticated remains false after login

User.Identity.IsAuthenticated returns false after SignInAsync invoked

NLog .NET Core aspnet-user-identity always empty

asp.net core identity custom user data with a collection

.NET FormsAuthentication and HttpContext.Current.User.Identity.IsAuthenticated combination not working

Custom Authentication using legacy user table in Asp.Net Core

Passport isAuthenticated() always returns false?

Identity signIn / PasswordSignIn returns success, but @User.Identity.IsAuthenticated returns false

.NET Core Identity Server 4 Authentication VS Identity Authentication

.NET Core Web API HttpContext.User.Claims and HttpContext.User.Identity are always null in Controllers

Sign out a user using AuthenticationStateProvider and set User.Identity.IsAuthenticated to false using Blazor

ASP.NET Identity - Steps for custom authentication