Solved

Error checking type for HttpContext.Current.User.Identity

Posted on 2009-05-20
2
910 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
2 Comments
 
LVL 12

Accepted Solution

by:
GuitarRich earned 500 total points
Comment Utility
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
Comment Utility
Apologies for late acceptance. The answer did the trick. Many thanks.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now