I use ASP.NET MVC 5
and Identity
for Authentication
, My problem is with belowe code :
User.Identity.IsAuthenticated
User
is Null , but I used Above code in View Like belowe and its work fine and user has Value .
@if (User.Identity.IsAuthenticated)
{...}
what is matter ?
to do this I search on google and find a way with
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
}
I suspect that you are trying to call this method inside the constructor of a controller. This constructor is called too early in the HTTP request execution pipeline and the HttpContext is not available there. You can access the HttpContext inside the Initialize method:
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
// Now you can access the HttpContext and User
if (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