Solved

Error checking type for HttpContext.Current.User.Identity

Posted on 2009-05-20
2
932 Views
Last Modified: 2013-12-17
This code was converted from VB.NET and I'm getting the following errors:

System.Web.HttpContext.Current' is a 'property' but is used like a 'type'
and
Cannot implicitly convert type 'System.Security.Principal.IIdentity' to 'System.Web.Security.FormsIdentity'. An explicit conversion exists (are you missing a cast?)
public void doAuthentication()
    {
        if (HttpContext.Current.User != null)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (typeof (HttpContext.Current.User.Identity) is FormsIdentity)
                {
                    FormsIdentity id = HttpContext.Current.User.Identity;
                    FormsAuthenticationTicket ticket = id.Ticket;
                    string strUserData = ticket.UserData;
                    string[] strRoles = strUserData.Split(',');
                    HttpContext.Current.User = new GenericPrincipal(id, strRoles);
                }
            }
        }
    }

Open in new window

0
Comment
Question by:derekclee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
GuitarRich earned 500 total points
ID: 24431153
Change it to this - you don't need the typeof in the if statement and you need to cast the Identity to a FormsItentity. This compiled ok for me - not tested it in runtime tho.

	public void doAuthentication()
	{
		if (HttpContext.Current.User != null)
		{
			if (HttpContext.Current.User.Identity.IsAuthenticated)
			{
				if (HttpContext.Current.User.Identity is FormsIdentity)
				{
					FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
					FormsAuthenticationTicket ticket = id.Ticket;
					string strUserData = ticket.UserData;
					string[] strRoles = strUserData.Split(',');
					HttpContext.Current.User = new GenericPrincipal(id, strRoles);
				}
			}
		}
	}

Open in new window

0
 

Author Closing Comment

by:derekclee
ID: 31583479
Apologies for late acceptance. The answer did the trick. Many thanks.
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question