There seems to be some ambiguity about these 2. I use them interchangeably throughout my project and the only reason is because I couldn't figure out when to use one or the other.
What would be the cases where one is true while the other one isn't?
If I were to only use ASP.NET Identity to authenticate users, would one or the other more favourable or it doesn't really matter?
It just holds the username of the user that is currently logged in. After login successful authentication, the username is automatically stored by login authentication system to "HttpContext.Current.User.Identity.Name" property.
Obviously, User. Identity. IsAuthenticated checks to see if the User is authenticated while Request. IsAuthenticated checks to see if the Request is Authenticated.
You can achieve this by manually settings HttpContext. User: var identity = new ClaimsIdentity("Custom"); HttpContext. User = new ClaimsPrincipal(identity);
identity. isauthenticated is False when a user is already logged in.
HttpContext.User.Identity.IsAuthenticated
checks if the current user is
authenticated(true if the user was authenticated; otherwise, false.). When you set user like this: HttpContext.User = new
ClaimsPrincipal(new ClaimsIdentity("SomeAuthType"));
or the request authentication is succeed for any authentication middleware(such as JwtBearer
) it will be
true.return principal?.Identities != null && principal.Identities.Any(i => i.AuthenticationType == Options.Cookies.ApplicationCookieAuthenticationScheme);
. So, if you use aspnet identity and you want to check if user is authenticated by aspnet identity middleware(by app.UseIdentity()
) then use SignInManager.IsSignedIn
. If you don't use aspnet identity or it is not important how the current user is authenticated then use HttpContext.User.Identity.IsAuthenticated
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With