How to check if user is authenticated in Razor pages of .Net Core 2.0

混江龙づ霸主 提交于 2020-03-17 09:52:37

问题


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:


Edit: David is right of course.

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

@if(!User.Identity.IsAuthenticated) 
{
    ...
}


来源:https://stackoverflow.com/questions/45888275/how-to-check-if-user-is-authenticated-in-razor-pages-of-net-core-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!