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

  • SEARCH
  • Home
  • 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 6907771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:28:36+00:00 2026-05-27T08:28:36+00:00

We use MVC 3. The default user management is not usable for us as

  • 0

We use MVC 3. The default user management is not usable for us as our account info is stored in our own data-store and access goes via our own repository classes.

I’m trying to assign a principal add roles to the HttpContext.User and give out an authorization cookie.

Based on a code snipped I found I tried something like this:

if (UserIsOk(name, password))
{
    HttpContext.User =
        new GenericPrincipal(
            new GenericIdentity(name, "Forms"),
            new string[] { "Admin" }
        );
    FormsAuthentication.SetAuthCookie(name, false);

    return Redirect(returnUrl);
}

When the next request is done, the user is authenticated, but he is not in the “Admin” role.
What am I missing?

  • 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-27T08:28:37+00:00Added an answer on May 27, 2026 at 8:28 am

    I think you should implement FormsAuthenticationTicket.
    More info here : http://msdn.microsoft.com/en-us/library/aa289844(v=vs.71).aspx

    In Mvc it is quite similar.

    I have a class called UserSession that is injected into LoginController and that I use in LogOn action :

        [HttpPost, ValidateAntiForgeryToken]
    public ActionResult Index(LoginInput loginInput, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            return (ActionResult)_userSession.LogIn(userToLog, loginInput.RememberMe, CheckForLocalUrl(returnUrl), "~/Home");
        }
    }
    

    Here’s my UserSession LogIn implementation (notice I put the “Admin” role hard coded for the example, but you could pass it as argument) :

    public object LogIn(User user, bool isPersistent, string returnUrl, string redirectDefault)
        {
            var authTicket = new FormsAuthenticationTicket(1, user.Username, DateTime.Now, DateTime.Now.AddYears(1), isPersistent, "Admin", FormsAuthentication.FormsCookiePath);
            string hash = FormsAuthentication.Encrypt(authTicket);
    
            var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
    
            if (authTicket.IsPersistent) authCookie.Expires = authTicket.Expiration;
    
            HttpContext.Current.Response.Cookies.Add(authCookie);
    
            if (!String.IsNullOrEmpty(returnUrl))
                return new RedirectResult(HttpContext.Current.Server.UrlDecode(returnUrl));
    
            return new RedirectResult(redirectDefault);
        }
    

    Then in the base controller I’ve overriden OnAuthorization method to get the cookie :

    if (filterContext.HttpContext.Current.User != null)
    {
       if (filterContext.HttpContext.Current.User.Identity.IsAuthenticated)
       {
          if( filterContext.HttpContext.Current.User.Identity is FormsIdentity ) 
          {
             FormsIdentity id = filterContext.HttpContext.Current.User.Identity as FormsIdentity;
             FormsAuthenticationTicket ticket = id.Ticket;
             string roles = ticket.UserData;
    
             filterContext.HttpContext.Current.User = new GenericPrincipal(id, roles);
          }
       }
    }
    

    I hope this helps. Let me know.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In ASP.NET MVC (default routing),I'd like to use a URL like this to return
I've recently set up an ASP.net site (not using MVC.net) to use URL Routing
I am trying to make the default ASP.NET MVC application use a remote Oracle
My ASP.NET MVC intranet app has a data repository that uses current user's Windows
I'm trying to use MVC for a new project after having been around the
I am a web and mobile application developer and I prefer to use MVC
MVC use action attributes to map the same view for http get or post:
I use Spring MVC [version: 2.5] and Security[version: 2.0.4]. My problem looks like that:
I use Spring MVC (via Spring Roo) to build a small web application for
I use Spring MVC 3.0 and JSP. I have an object: public class ObjectWrapper

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.