asp.net-mvc 检查用户是否已登录用户控件 Asp.net MVC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1332132/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:47:51  来源:igfitidea点击:

check if user is logged in in user control Asp.net MVC

asp.net-mvcuser-controls

提问by Hannoun Yassir

how can i check if a user is logged in in user control with asp.net mvc

我如何检查用户是否使用 asp.net mvc 在用户控件中登录

usually on a view page i use this

通常在查看页面上我使用这个

<% if (User.Identity.IsAuthenticated) {%>
  //Do something
<% } %>

but i can't get this done on a user control

但我无法在用户控件上完成此操作

回答by griegs

Does this work?

这行得通吗?

<%= Page.User.Identity.IsAuthenticated %>

回答by Dan Atkinson

Nothing new to add to Griegs answer, but I would normally do

没有什么新东西可以添加到 Griegs 的答案中,但我通常会这样做

@Request.IsAuthenticated

回答by CmdrTallen

You could decorate the Method with the Authorizeattribute. This requires that the User calling the Method being authenticated.

您可以使用Authorize属性装饰方法。这要求调用方法的用户进行身份验证。

回答by Eshan

Well I use VB

好吧,我用VB

If User.Identity.Name = "" Then
   Response.Redirect("~/Login.aspx")
Else
   ........continue...........
End If