c# - 从 C# 类库验证用户 - MVC

标签 c# asp.net-mvc authentication

不确定这是否可能,但我们开始吧:

我们的 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/

相关文章:

asp.net-mvc - 复杂模型绑定(bind)

javascript - 无法通过 javascript 链接回 MVC View

cakephp - 强制 cakephp 3 auth->user() 显示虚拟字段

RESTful 认证

c# - 在 WCF 服务契约(Contract)中使用 soapAction =""进行多项操作?

c# - 在 UWP 应用程序中检测 sleep 事件或唤醒事件

javascript - 如何在 type=textbox 的 div 中输入文本?

c# - 如何在 WPF 中将标签文本居中?

c# - URL 只是没有显示我想要的

java - Dynamics CRM 2016.Web API 授权。我有 token ,现在呢?