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
Solved

Error checking type for HttpContext.Current.User.Identity

Posted on 2009-05-20
2
923 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

791 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