简体   繁体   English

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

[英]ASP.NET MVC 2 Authorize ONLY unauthenticated users

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. 我在我的站点地图中显示了一个登录标签,我只希望它在用户尚未登录时显示。

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. 您所要求的并不完全像授权 - 在我看来,授权用户(在这种情况下是未经身份验证的用户)将被提供给ActionResult (在这种情况下是一个视图),而未经授权的用户则不会。 In what you describe, the ActionResult is returned for all users; 在您描述的内容中,为所有用户返回ActionResult ; 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. 您可能只想检查User.Identity.IsAuthenticatedRequest.IsAuthenticated ,如果未经身份验证,请为您的登录标签发出HTML。 You might want ot put this into an MVC UserControl 您可能希望将其放入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"); %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM