?
Solved

Error checking type for HttpContext.Current.User.Identity

Posted on 2009-05-20
2
Medium Priority
?
936 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

752 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