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

C# ASP.NET Forms Authentication IsInRole Returns False

Posted on 2012-04-04
4
832 Views
Last Modified: 2012-04-10
I have a web site that is acting really strange and I'm not sure where the issue. The website is almost an exact copy of a previous website that still works.

For some reason the IsInRole is returning false even though it shouldn't.

I run this code:

FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;

// Get the stored user-data, in this case, our roles
string userData = ticket.UserData;
string[] roles = userData.Split(',');

Literal3.Text = roles[0];

// Checking for "System Administrator"

Literal3.Text += "|" + Page.User.IsInRole(roles[0]).ToString();
Literal3.Text += "|" + HttpContext.Current.User.IsInRole(roles[0]).ToString();

Open in new window


And it actually spits out: System Administrator|False|False

Which makes no sense to me, since I'm basically hard coding it to give out a response of true. For kicks I tested this on my app that works and I got True values. I've tried messing with IIS, scrubbing the Web.Config file and copying from my application that still works fine, and various other things. I'm down to the point where I'll likely end up re-creating the whole thing.

I'm hoping someone can give me a lightbulb as to why the IsInRole is acting up.
0
Comment
Question by:mis_greenwoodhall
  • 3
4 Comments
 
LVL 9

Expert Comment

by:richard_hughes
ID: 37809478
Hello mis_greenwoodhall

Do you know if both websites are running against the same database?

Thanks,

Richard Hughes
0
 
LVL 1

Author Comment

by:mis_greenwoodhall
ID: 37809540
No, they are actually running against different databases.

However, they are running against the same schema and data; one was copied from the other.
0
 
LVL 1

Accepted Solution

by:
mis_greenwoodhall earned 0 total points
ID: 37812454
I figured out my own problem. For some reason the Global.asax file was not being created. I can't recall if I created that myself on the old app. But adding the Global.asax file with this section fixed my issue:

    protected void Application_AuthenticateRequest(Object sender,
    EventArgs e)
    {
        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;

                    // Get the stored user-data, in this case, our roles
                    string userData = ticket.UserData;
                    string[] roles = userData.Split(',');
                    HttpContext.Current.User = new GenericPrincipal(id, roles);
                }
            }
        }
    }    

Open in new window

0
 
LVL 1

Author Closing Comment

by:mis_greenwoodhall
ID: 37826768
No other solution was offered.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

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…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

837 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