Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6882685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:17:10+00:00 2026-05-27T05:17:10+00:00

I know this has been answered here before, however even after following all the

  • 0

I know this has been answered here before, however even after following all the solutions I could find, I cannot still get my roles working in my system.

I have a Asp.Net MVC application, with Forms based authentication. Instead of using a local database, it uses OpenAuth/OpenID for authentication, and a database lookup table for application roles.

As per main suggestion, I implemented the roles in Global.asax like:

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    //Fires upon attempting to authenticate the use
    if (HttpContext.Current.User != null &&
        HttpContext.Current.User.Identity.IsAuthenticated &&
        HttpContext.Current.User.Identity.GetType() == typeof (FormsIdentity))
        Thread.CurrentPrincipal = HttpContext.Current.User = OpenAuthPrincipal.Get(HttpContext.Current.User.Identity.Name);
}

Here OpenAuthPrincipal.Get is a very straightforward static method wrapping the openauth id with the roles:

public static IPrincipal Get(string userId)
{
    var db = new WebData();
    var user = db.Users.Find(userId);

    return new GenericPrincipal(new Identity(user), user.Roles.Split('|'));
}

However when I reach a function like:

[Authorize(Roles = "Admin")]
public ActionResult Edit(int id)
{
        ...
}

It fails. If I remove the Roles restriction, and check User.IsInRole("Admin") in the debugger I get a false. However, if I do the check in the Global.asax, I get true.

I know that the User.Identity.Name is coming correctly. And also the IIdentity is not modified at all. However only the roles are lost.

What could be the cause of this issue?

Update:

The solution recommended below did not directly work, however this change fixed the issue for me:

protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
{
    httpContext.User = OpenAuthPrincipal.Get(httpContext.User.Identity.Name);

    return base.AuthorizeCore(httpContext);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:17:10+00:00Added an answer on May 27, 2026 at 5:17 am

    As per main suggestion, I implemented the roles in Global.asax like:

    No idea where did you get this main suggestion from but in ASP.NET MVC you normally use authorization action filters. And since the default Authorize filter doesn’t do what you need, you write your own:

    public class OpenIdAuthorizeAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var authorized = base.AuthorizeCore(httpContext);
            if (authorized)
            {
                httpContext.User = OpenAuthPrincipal.Get(httpContext.User.Identity.Name);
            }
            return authorized;
        }
    }
    

    and then:

    [OpenIdAuthorize(Roles = "Admin")]
    public ActionResult Edit(int id)
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this has been answered before and why this is caused. However, in
Sorry if this question has been answered before; however all of the questions that
First of all, i know this question has been sort of asked/sort-of answered here:
I don't know if this has been answered before(i looked online but couldn't find
I know this type of question has been asked and answered before but I
I know this has been asked before but all the answers I found didn't
I know this question has been asked before and I read all the answers
I know this question has been partly answered here on S.O. , but there
I know that something similar to this has been answered before but its not
I know a similar question has been answered before here , but I just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.