Where can I get minimum "hello world" .aspx template with UserRoles (hopefully with MVC/CRUD operations on mssql userRole tables)?

Ivan 1 Reputation point
2021-10-22T17:43:18.743+00:00

I am struggling to find any manual to make minimal asp-net application, based on .aspx WebForms template with ms-sql used for ROLES.

When I create new asp-net site (.aspx pages with register, login etc), I don't see any "roles" pages there.
Also it seems that by default feature of having different roles in such template is turned off.
then when I try to add roles, I've got errors that I need some "roles provider".
But I'ld like to use built-in default roles provider. I don't need custom tables for users, and their roles, and table non need of non-standard ms-sql-table which matches users and roles tables.
then why I need custom role provider?

I can't find any good manual on this. Either roles manager examples are too big (and thus not running, especially MVC), or they seem to be old (use .aspx). Thus I cannot even make simple app with roles management.

Worst thing is that when I add in web.config allow only admin user, and deny other users, this works only for .aspx pages, but files of any other kind are available to any non-admin user... is this because I use so-called "old" .aspx/WebForms technology? Should I use Non-visual technology (cshtml)? I thus don't see any reason for Visual Studio to be called "Visual", if there are no .aspx visual designer of .cs pages ("back-code", code on back-end on server - "runat=server").

I cannot find hello-world app on Microsoft MSDN site for making roles project for aspx pages (using mssql).

please if you have any good hello-world manual for aspx user-roles, post it here (hope with source code, I need just very basic mssql db with default roles, user roles, and simply use c# functions to access roles, to add new roles, remove roles, show them, check who is in which role, I mean CRUD operatins on MSSQL user/roles tables)

if there is possible to get any simplest "hello-world" MVC template to for roles-editing (CRUD operations for asp-net user roles tables) - it is even better. If this MVC crud editor for roles can work with .aspx webForms - it is still even more wanted and welcome.

Thank you in advance

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,244 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 26,186 Reputation points
    2021-10-22T20:18:21.683+00:00

    I assume you created a new Web Forms project using the "Individual Account" option. This adds ASP.NET Identity to the project which is an API and Database for working with user accounts. The API is made up of several services to interact with the database. The template configures two of the services; SignInManager and UserManager.

    There is a RoleManager for adding/updating/deleting roles in the Roles table. Not to be confused with adding Roles to User which is a UserManager feature. Typically applications only have a hand full of Roles which is a one time script to add the roles. These days Claims are used to describe a user which the UserManager handles.

    Anyway, there are tons of ASP.NET Identity tutorials and blogs which is really your main question.

    Introduction to ASP.NET Identity

    Edit: code sample.

    Add an ApplicationRoleManager class to the IdentityConfig.cs file.

    public class ApplicationRoleManager : RoleManager<IdentityRole>  
    {  
        public ApplicationRoleManager(IRoleStore<IdentityRole, string> store) : base(store)  
        {  
        }  
        public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)  
        {  
            var roleStore = new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>());  
            return new ApplicationRoleManager(roleStore);  
        }  
    }  
    

    Register the ApplicationRoleManager in the Startup.Auth.cs file.

    public void ConfigureAuth(IAppBuilder app)  
    {  
        // Configure the db context, user manager and signin manager to use a single instance per request  
        app.CreatePerOwinContext(ApplicationDbContext.Create);  
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);  
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);  
        app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);  
    

    Get the ApplicationRoleManager service in the code behind of your customer add/remove roles page.

    protected void Button1_Click(object sender, EventArgs e)  
    {  
        ApplicationRoleManager roleManager = Context.GetOwinContext().Get<ApplicationRoleManager>();  
        roleManager.Create(new Microsoft.AspNet.Identity.EntityFramework.IdentityRole("Admin"));  
    }  
    

    Again, the RoleManager just works with the AspNetRoles table. Typically, there are only a few roles in any application, Admin, User, SuperUser, which is a one time script. Use Claims if you need many ways to describe a user.

    0 comments No comments