Adding User Roles to Visual Studio Web Application (Web Forms) RRS feed

  • Question

  • I'm writing a new application and when I created the project initially using the template, it included the default user management features provided by ASP.NET. I'm not very familiar with this and have struggled to find documentation that wasn't part of an MVC application tutorial.

    I've used a few tutorials and have an extended ApplicationRole class.

    public class ApplicationRole : IdentityRole
        public ApplicationRole() : base() { }
        public ApplicationRole(string name) : base(name) { }
        public string Description { get; set; }

    In the tutorials and template there is an IdentityConfig.cs file that has various classes including one for the ApplicationUserManager and I created an ApplicationRoleManager class using the code below:

    public class ApplicationRoleManager : RoleManager<ApplicationRole>
        public ApplicationRoleManager(
            IRoleStore<ApplicationRole, string> roleStore)
            : base(roleStore)
        public static ApplicationRoleManager Create(
            IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
            return new ApplicationRoleManager(
                new RoleStore<ApplicationRole>(context.Get<ApplicationDbContext>()));

    The problem comes with the first step: creating a new role. I'm trying to check if a role exists before I create it and thought the code (in the .aspx.cs file) below would work however I keep getting exceptions (pasted below the code)

    protected void CreateRoleButton_Click(object sender, EventArgs e)
        string newRoleName = RoleName.Text.Trim();
        var manager = Context.GetOwinContext().GetUserManager<ApplicationRoleManager>();
            var role = new ApplicationRole(newRoleName);
        RoleName.Text = string.Empty;


    Server Error in '/' Application.
    Value cannot be null.
    Parameter name: manager
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    Exception Details: System.ArgumentNullException: Value cannot be null.
    Parameter name: manager
    Source Error:
    Line 29:            
    Line 30: 
    Line 31:             if(manager.RoleExists(newRoleName))
    Line 32:             {
    Line 33:                
    Source File: C:\Users\Administrator\Source\Repos\QuickQuote\QuickQuote\QuickQuote\Roles\ManageRoles.aspx.cs    Line: 31
    Stack Trace:
    [ArgumentNullException: Value cannot be null.
    Parameter name: manager]
       Microsoft.AspNet.Identity.RoleManagerExtensions.RoleExists(RoleManager`2 manager, String roleName) +155
       QuickQuote.Roles.ManageRoles.CreateRoleButton_Click(Object sender, EventArgs e) in C:\Users\Administrator\Source\Repos\QuickQuote\QuickQuote\QuickQuote\Roles\ManageRoles.aspx.cs:31
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9819334
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639

    I'm assuming that the manager object isn't working as I'd expect and realise this is where I'm stuck. How do I create a valid extended IdentityRole object that I can use the RoleExists method on?

    • Moved by CoolDadTx Friday, August 11, 2017 2:37 PM ASP.NET related
    Friday, August 11, 2017 9:15 AM

All replies

  • I'm assuming that the manager object isn't working as I'd expect and realise this is where I'm stuck. How do I create a valid extended IdentityRole object that I can use the RoleExists method on?

    The ASP.NET Identity in trying to get the template code to work is a total mess. It took me a day just to get it to register a new user with Unity implemented for IoC and dependency injection.

    I might find another framework that works that replaces the whole thing.   

    Anyway, you have Web form and the Security sections in the ASP.NET forum that may help you.


    • Proposed as answer by Just Karl Tuesday, August 15, 2017 3:03 PM
    Friday, August 11, 2017 11:02 AM