Solved

C# ASP.NET Forms Authentication IsInRole Returns False

Posted on 2012-04-04
4
826 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

832 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