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 354997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:58:36+00:00 2026-05-12T11:58:36+00:00

I am building a web application using ASP.NET MVC that has two very distinct

  • 0

I am building a web application using ASP.NET MVC that has two very distinct types of users. I’ll contrive an example and say that one type is content producers (publishers) and another is content consumers (subscribers).

I am not planning on using the built-in ASP.NET authorization stuff, because the separation of my user types is a dichotomy, you’re either a publisher or a subscriber, not both. So, the build-in authorization is more complex than I need. Plus I am planning on using MySQL.

I was thinking about storing them in the same table with an enum field (technically an int field). Then creating a CustomAuthorizationAttribute, where I’d pass in the userType needed for that page.

For example, the PublishContent page would require userType == UserType.Publisher, so only Publishers could access it. So, creating this attribute gives me access to the HttpContextBase, which contains the standard User field (of type IPrincipal). How do I get my UserType field onto this IPrincipal? So, then my attribute would look like:

public class PublisherAuthorizationAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (!httpContext.User.Identity.IsAuthenticated)
            return false;

        if (!httpContext.User.Identity.UserType == UserTypes.Publisher)
            return false;

        return true;
    }
}

Or does anyone think my entire approach is flawed?

  • 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-12T11:58:36+00:00Added an answer on May 12, 2026 at 11:58 am

    I would still use the built in ASP.NET forms authentication but just customize it to your needs.

    So you’ll need to get your User class to implement the IPrincipal interface and then write your own custom cookie handling. Then you can simply just use the built in [Authorize] attribute.

    Currently I have something similar to the following…

    In my global.asax

    protected void Application_AuthenticateRequest()
    {
        HttpCookie cookie = Request.Cookies.Get(FormsAuthentication.FormsCookieName);
        if (cookie == null)
            return;
    
        bool isPersistent;
        int webuserid = GetUserId(cookie, out isPersistent);
    
        //Lets see if the user exists
        var webUserRepository = Kernel.Get<IWebUserRepository>();
    
        try
        {
            WebUser current = webUserRepository.GetById(webuserid);
    
            //Refresh the cookie
            var formsAuth = Kernel.Get<IFormsAuthService>();
    
            Response.Cookies.Add(formsAuth.GetAuthCookie(current, isPersistent));
            Context.User = current;
        }
        catch (Exception ex)
        {
            //TODO: Logging
            RemoveAuthCookieAndRedirectToDefaultPage();
        }
    }
    
    private int GetUserId(HttpCookie cookie, out bool isPersistent)
    {
        try
        {
            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
            isPersistent = ticket.IsPersistent;
            return int.Parse(ticket.UserData);
        }
        catch (Exception ex)
        {
            //TODO: Logging
    
            RemoveAuthCookieAndRedirectToDefaultPage();
            isPersistent = false;
            return -1;
        }
    }
    

    AccountController.cs

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult LogOn(LogOnForm logOnForm)
    {
        try
        {
            if (ModelState.IsValid)
            {
                WebUser user = AccountService.GetWebUserFromLogOnForm(logOnForm);
    
                Response.Cookies.Add(FormsAuth.GetAuthCookie(user, logOnForm.RememberMe));
    
                return Redirect(logOnForm.ReturnUrl);
            }
        }
        catch (ServiceLayerException ex)
        {
            ex.BindToModelState(ModelState);
        }
        catch
        {
            ModelState.AddModelError("*", "There was server error trying to log on, try again. If your problem persists, please contact us.");
        }
    
        return View("LogOn", logOnForm);
    }
    

    And finally my FormsAuthService:

    public HttpCookie GetAuthCookie(WebUser webUser, bool createPersistentCookie)
    {
        var ticket = new FormsAuthenticationTicket(1,
                                                   webUser.Email,
                                                   DateTime.Now,
                                                   DateTime.Now.AddMonths(1),
                                                   createPersistentCookie,
                                                   webUser.Id.ToString());
    
        string cookieValue = FormsAuthentication.Encrypt(ticket);
    
        var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieValue)
                             {
                                 Path = "/"
                             };
    
        if (createPersistentCookie)
            authCookie.Expires = ticket.Expiration;
    
        return authCookie;
    }
    

    HTHs
    Charles

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

Sidebar

Ask A Question

Stats

  • Questions 290k
  • Answers 290k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Obviously i don't know the exact details of your situation… May 13, 2026 at 5:45 pm
  • Editorial Team
    Editorial Team added an answer To link to Rails' standard "show" action for the video:… May 13, 2026 at 5:45 pm
  • Editorial Team
    Editorial Team added an answer From the Google App Engine docs for the Response object:… May 13, 2026 at 5:45 pm

Related Questions

I am currently building an application using ASP.NET MVC. The data entry pages are
I am building an ASP.NET MVC application using the 1.0 release using Visual Web
I'm am building my asp.net web application using MVC (Preview 5), and am also
I'm a .NET and MVC newbie, and learning it for the first time after

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.