ASP.NET MVC 2 仅授权未经身份验证的用户
我试图找出如何只授权未经身份验证的用户。我的站点地图中显示了一个登录选项卡,我只希望它在用户尚未登录时显示。
im trying to figure out how to authorize only unauthenticated users. i have a sign in tab displayed in my site map and i only want it to show up when the user hasnt yet logged in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所要求的听起来不太像授权 - 在我看来,授权用户(在本例中为未经身份验证的用户)将获得
ActionResult
(在本例中为视图),而未经授权的用户将获得ActionResult
(在本例中为视图)用户不会。根据您的描述,将为所有用户返回ActionResult
;我们只是想从您的选项卡向未经身份验证的用户发送额外的 html。您可能只想检查
User.Identity.IsAuthenticated
或Request.IsAuthenticated
,如果未经身份验证,则发出登录选项卡的 HTML。您可能不想将其放入 MVC UserControl将用户控件放入共享文件夹中,然后在像这样的视图中使用
What you're asking doesn't quite sound like authorisation - in my opinion, an authorised user (in this case an unauthenticated user) would be served the
ActionResult
(in this case a view) whereas an unauthorised user would not. In what you describe, theActionResult
is returned for all users; we just want to emit additional html fr your tab to unauthenticated users.you might just want to check
User.Identity.IsAuthenticated
orRequest.IsAuthenticated
and if unauthenticated, emit the HTML for your sign in tab. You might want ot put this into an MVC UserControlPut the user control in the shared folder then use in a view like so