How to check if user is authenticated in Razor pag

2020-05-29 13:29发布

I would like to check if a user is logged in in an ASP.NET Core 2.0 application in a Razor page. The following code worked in .NET 4.6.1:

@if (!Request.IsAuthenticated)
{
    <p><a href="@Url.Action("Login", "Account")" class="btn btn1-success btn-lg" role="button" area="">Sign In &raquo;</a></p>
}

How can I do this in Core 2.0?

1条回答
可以哭但决不认输i
2楼-- · 2020-05-29 13:45

Edit: David is right of course.

Just check if User or HttpContext.User.Identity.IsAuthenticated is true or not.

@if(!User.Identity.IsAuthenticated) 
{
    ...
}
查看更多
登录 后发表回答