ASP.NET MVC 5 Security And Creating User Role

In this article we will see how to use ASP.NET Identity in MVC Application for creating user roles and displaying the menu depending on user roles.Here we will see how to:  Create default admin role and other roles. Create default admin users. Add Username for new User Registrat

C# (1.5 MB)
 
 
 
 
 
4.2 Star
(38)
49,673 times
Add to favorites
1/21/2019
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • keep getting this error
    1 Posts | Last post February 28, 2019
    • 
      ApplicationDbContext db = new ApplicationDbContext(); ---------I keep getting an error right here.
      
      when i run the code. The error is displayed at the semicolon.
      
      this is what is displayed(below). Can anyone help
      
      An exception of type 'System.TypeInitializationException' occurred in EntityFramework.dll but was not handled in user code
      
      Additional information: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
  • .Models had red underline
    1 Posts | Last post January 02, 2019
    • Hi my .models had red underline may i know why?
  • is Not maped
    1 Posts | Last post May 01, 2018
    • Hi 
      I run the project log in is success but add to other table (category,product..vs) run project
      
      error 2063: At least one property must be mapped in the set mapping for 'AspNetUserRoles'.'
      
      
  • Invalid login attempt
    1 Posts | Last post January 23, 2018
    • Run the project of "shanuMVCUserRoles" , log in with the user name "syedshanumcain@gmail.com" and the password "A@Z200711" account, prompting "Invalid login attempt".
      
      Thanks!
  • 555-555-0199@example.com
    2 Posts | Last post November 15, 2017
  • What does this error mean
    1 Posts | Last post September 29, 2017
    • 
      Invalid column name 'UserId'.
      Invalid column name 'UserId'. 
        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.Data.SqlClient.SqlException: Invalid column name 'UserId'.
      Invalid column name 'UserId'.
      
      Source Error: 
      
      
      
      Line 158:            {
      Line 159:                var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
      Line 160:                var result = await UserManager.CreateAsync(user, model.Password);
      Line 161:                if (result.Succeeded)
      Line 162:                {
        
      
       Source File:  H:\Project Folder\Project2\Project2\Controllers\AccountController.cs    Line:  160 
      
  • Incomplete code?
    3 Posts | Last post September 11, 2017
    • Hi... i've been trying this code and works ALMOST flawless, there is a small detail on the Controllers\RoleController.cs you wrote this code:
      
      ApplicationDbContext context = new ApplicationDbContext();
      public ActionResult Index()
              {
                  if (User.Identity.IsAuthenticated)
                  {
                      if (!isAdminUser())
                      {
                          return RedirectToAction("Index", "Home");
                      }
                  }
                  else
                      return RedirectToAction("Index","Home");
      
                  var Roles = context.Roles.ToList();
                  return ViewBag(Roles);
              }
      
      BUT where is the isAdminUser()? trying to fix the code i wrote this:
      
      private bool isAdminUser()
              {
                  if (User.Identity.IsAuthenticated)
                  {
                      var user = User.Identity;
                      
                      var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
                      var s = UserManager.GetRoles(user.GetUserId());
      
                      if (s[0].ToString() == "Admin")
                      {
                          return true;
                      }
                      else
                          return false;
                  }
                  return false;
              }
      
      But it just keeps giving me errors... any chance you can complete the code for this article?
      
      Thank you!
    • Have you tried break point? 
    • The RoleController in the example/walkthrough did not include the additional actions needed to make it function. View the RoleController on the Browse Code tab above to see additional actions. 
  • Registered user, belonging to a role, can't enter action method!
    4 Posts | Last post September 08, 2017
    • Sir,
      
      I modified my "POST: /Account/Register / public async Task<ActionResult> Register(RegisterViewModel model)" [AccountController] action method just as your article says to under the "Register User" section (well, except that I'm redirecting the user to /Home/Index), but I seem to be having a problem. 
      
      Even though I'm adding all newly registered users to the "PropertyOwner" role, as soon as I, as the newly registered user try to go to a method with an Authorize attribute specifying the roles a user must belong to, e.g. [Authorize(Roles = "Admin, PropertyOwner")], I get sent back to the login screen! It seems to me that I should be able to use the method without issue.
      
      Any chance you can help me fix this? Or know what the problem is?
      
      
      Thanks!
      
    • I found the answer. I was looking around for said answer and found this StackOverflow post.
      
      http://stackoverflow.com/questions/19689183/add-user-to-role-asp-net-identity
      
      In the first answer, they're also showing a modified Register(RegisterViewModel model) method. But in that person's answer, the user is added to the role BEFORE the call to SignInAsync(). So, I moved my SignInAsync call to AFTER the user is added to the role and it works great!
      
    • Could you please elaborate on your question?  
      
      What does this achieve:
      I moved my SignInAsync call to AFTER the user is added to the role and it works great!
    • http://stackoverflow.com/questions/19689183/add-user-to-role-asp-net-identity
  • Thanks for a compact application extension. In accounts controller, 'GET: /Account/Register" and " POST: /Account/Register" props up error CS0103 The Name does not exist in the current context. Pl advise what to be done?
    2 Posts | Last post September 08, 2017
    • Thanks for a compact application extension. In accounts controller,  'GET: /Account/Register" and " POST: /Account/Register" props up error CS0103 The Name does not exist in the current context. Pl advise what to be done?
    • You need to create data in SQLServer.
  • What is the point of creating roles by the end-user?
    2 Posts | Last post September 08, 2017
    • Why the ends user ever wants to create roles if he wouldn't be able to give authorization to users assigned to these roles, it should be done in code.
    • Coding are many ways, not only way suit one sitation. Coding is to solve what we want.
1 - 10 of 23 Items