开发者

ASP.NET MVC 2 Authorize ONLY unauthenticated users

im trying to figure out how to autho开发者_如何转开发rize 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. 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"); %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜