不确定这是否可能,但我们开始吧:
我们的 MVC 网站目前已经过重新设计。以前,我们将登录按钮作为图像,如果用户通过身份验证,则会显示注销按钮。像这样:
<%
if (Request.IsAuthenticated)
{
%>
<a href="/Account/LogOff">
<img src="/images/logout.png" alt="logout" border="0" />
</a>
<%
}
else
{
%>
<a href="<%: Url.Action("LogOn","Account")%>">
<img src="/Images/login.png" alt="Log On" border="0" />
</a>
<%
}
%>
然而,网站的设计方式是,登录按钮现在包含在导航菜单中。由于站点中有多个区域,我们使用 Helper 类中的 c# 方法类从站点地图生成菜单,如下所示:
public static string TabbedMenu(this HtmlHelper html, string area)
{
// Get all the current information.
//
RouteData route = html.ViewContext.RequestContext.RouteData;
string controller = route.GetRequiredString("controller");
string action = route.GetRequiredString("action");
StringBuilder menuWrapper = new StringBuilder();
menuWrapper.Append("<ul id=\"main-nav\" class=\"nav fl\">");
// Using the sitemap, build a tabbed menu.
//
foreach (SiteMapNode node in SiteMap.RootNode.ChildNodes)
{
if (node.Title == area)
{
foreach (SiteMapNode node2 in node.ChildNodes)
{
if (node2["controller"].ToLower() == controller.ToLower())
{
menuWrapper.Append("<li class=\"menu-item current-menu-item\">");
}
else
{
menuWrapper.Append("<li class=\"menu-item\">");
}
RouteValueDictionary values = new RouteValueDictionary(new { Action = node2["action"], Controller = node2["controller"], Area = node2["area"] });
VirtualPathData vpd = RouteTable.Routes.GetVirtualPathForArea(html.ViewContext.RequestContext, values);
string target = vpd.VirtualPath;
menuWrapper.AppendFormat("<a href=\"{0}\">{1}</a>", target, node2.Title);
menuWrapper.Append("</li>");
}
break;
}
}
menuWrapper.Append("<li id=\"menu-item-143\" class=\"login menu-item menu-item-type-custom menu-item-object-custom menu-item-143\"><a href=\"#\">Login</a></li>");
menuWrapper.Append("<li id=\"menu-item-333\" class=\"menu-item menu-item-type-custom menu-item-object-custom menu-item-333\"><a href=\"#\">Sign up</a></li>");
menuWrapper.Append("</ul>");
return menuWrapper.ToString();
}
所以我的问题真的是,有没有办法从这个辅助方法中对用户进行身份验证?
如有任何帮助,我们将不胜感激
谢谢!
最佳答案
你能给那个函数添加新的参数吗:
public static string TabbedMenu(this HtmlHelper html, string area, bool IsAuthenticated)
{
...
if(IsAuthenticated)
print something...
else
print something else...
...
}
关于c# - 从 C# 类库验证用户 - MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7307101/