asp.net-mvc 如何在 .Net Core 2.0 的 Razor 页面中检查用户是否通过身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45888275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if user is authenticated in Razor pages of .Net Core 2.0
提问by Roddy Balkan
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:
我想检查用户是否登录到 Razor 页面中的 ASP.NET Core 2.0 应用程序。以下代码适用于 .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 »</a></p>
}
How can I do this in Core 2.0?
我怎样才能在 Core 2.0 中做到这一点?
回答by Tseng
Edit: David is right of course.
编辑:大卫当然是对的。
Just check if Useror HttpContext.User.Identity.IsAuthenticatedis trueor not.
只是检查User或者HttpContext.User.Identity.IsAuthenticated是true或不是。
@if(!User.Identity.IsAuthenticated)
{
...
}

