ASP.NET MVC 2 仅授权未经身份验证的用户

发布于 2024-08-31 22:06:21 字数 61 浏览 2 评论 0原文

我试图找出如何只授权未经身份验证的用户。我的站点地图中显示了一个登录选项卡,我只希望它在用户尚未登录时显示。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓦然回首 2024-09-07 22:06:21

您所要求的听起来不太像授权 - 在我看来,授权用户(在本例中为未经身份验证的用户)将获得 ActionResult (在本例中为视图),而未经授权的用户将获得 ActionResult (在本例中为视图)用户不会。根据您的描述,将为所有用户返回 ActionResult ;我们只是想从您的选项卡向未经身份验证的用户发送额外的 html。

您可能只想检查 User.Identity.IsAuthenticatedRequest.IsAuthenticated,如果未经身份验证,则发出登录选项卡的 HTML。您可能不想将其放入 MVC UserControl

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
    if (!Request.IsAuthenticated) {
%>
        <!-- html here for your sign in tab -->
<%
    }
    else {
%> 
        <!-- possibly want a sign out tab here for authenticated users? -->
<%
    }
%>

将用户控件放入共享文件夹中,然后在像这样的视图中使用

<% Html.RenderPartial("Name of User Control"); %>

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, the ActionResult 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 or Request.IsAuthenticated and if unauthenticated, emit the HTML for your sign in tab. You might want ot put this into an MVC UserControl

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
    if (!Request.IsAuthenticated) {
%>
        <!-- html here for your sign in tab -->
<%
    }
    else {
%> 
        <!-- possibly want a sign out tab here for authenticated users? -->
<%
    }
%>

Put the user control in the shared folder then use in a view like so

<% Html.RenderPartial("Name of User Control"); %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文