Solved

C# ASP.NET Forms Authentication IsInRole Returns False

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Not showing page correctly 3 31
how to read json value 2 32
Reset asp.net login password 4 34
Where to download and how to install sqldmo.dll 5 38
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

863 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

24 Experts available now in Live!

Get 1:1 Help Now