开发者

ASP.Net MVC checking role of user in aspnet memebership

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
    if (Request.IsAuthenticated) {
%>
        Welcome <b><%= Html.Encode(Page.User.Identity.Name) %></b>!
        [ <%= Html.ActionLink("Log Off", "LogOff", "Account") %> ]
        <br /> 
<% if(User.IsInRole("Administrator")) { %>
        <br />
        <%= Html.ActionLink("Home", "Index", "Home") %> | <%= Html.ActionLink("About", "About", "Home") %> | <%= Html.A开发者_JAVA技巧ctionLink("UserControl","UserControl","Account")%>
        <% } else { %>
        <br />
        <%= Html.ActionLink("Home", "Index", "Home") %> | <%= Html.ActionLink("About", "About", "Home") %>
     <%} %>

I want to check the role of the user if it is an administrator only then the user can see the link to user control.when i try to run this code it is giving me an error saying "'Data.User' does not contain a definition for 'IsInRole'"


I don't believe the ViewUserControl type has a User property, so the compiler thinks you're trying to access your domain object of type Data.User. In a partial control, you can access the User instance using HttpContext.Current.User where the IsInRole method should work.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜