User.Identity.IsAuthenticated is always false in user authentication .net

Can someone check the code below and tell me why I always get false (User.Identity.IsAuthenticated) ??. I correctly receive cookies in my browser and am able to get the value from the application, but "User.Identity.IsAuthenticated" is 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

+9
source share
3 answers

ClaimsIdentity.IsAuthenticatedreturns falsewhen it ClaimsIdentity.AuthenticationTypeis null or empty. To avoid this, stop using the constructor without parameters ClaimsIdentityand use the overload parameter, which takes a parameter authenticationType:

var userIdentity = new ClaimsIdentity("Custom");
+20
source

. app.UseAuthentication() app.UseMvc(). , .

+1

You can try this. I think this may help.

var userIdentity = new ClaimsIdentity( claims, AuthenticationTypes.Basic);

0
source

Source: https://habr.com/ru/post/1682056/


All Articles