Solved

C# ASP.NET Forms Authentication IsInRole Returns False

Posted on 2012-04-04
4
813 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
No other solution was offered.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

6 Experts available now in Live!

Get 1:1 Help Now