109221793
109221793

Reputation: 16897

Authenticate User from c# Class Library - MVC

Not sure if this is possible but here we go:

Our MVC website has currently been redesigned. Previously, we had the had the Logon button as an image, and if the user was authenticated, a logout button would be displayed. Like so:

<%
    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>
<%
    }
%>

The way the website has been designed however, the login button is now incorporated in the navigation menu. As we have multiple areas within the site, we use a c# method class from within a Helper class to generate the menus from the sitemap, like so:

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();
}

So my question really is, is there any way to authenticate the user from within this helper method?

Any help would be appreciated,

Thanks!

Upvotes: 1

Views: 1439

Answers (2)

Waqas
Waqas

Reputation: 6802

In you Class library project add the reference to System.Web.dll which allow you to access the HttpRequest like this:

System.Web.HttpContext.Current.Request.IsAuthenticated

Upvotes: 3

ilija veselica
ilija veselica

Reputation: 9574

Can you add new parameter to that function:

public static string TabbedMenu(this HtmlHelper html, string area, bool IsAuthenticated)
{
     ...
     if(IsAuthenticated)
         print something...
     else
         print something else...
     ...
}

Upvotes: 3

Related Questions