Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# ASP.NET Forms Authentication IsInRole Returns False

Posted on 2012-04-04
4
Medium Priority
?
862 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
[X]
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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

610 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