MVC 4 Intranet Authentication with Custom Roles

后端 未结 1 539
野性不改
野性不改 2021-01-31 20:56

I have spent some time searching and found a lot of confusing answers, so I will post here for clarification.

I am using MVC4 VS2012 created an Intranet site using domai

相关标签:
1条回答
  • 2021-01-31 21:44

    I'm using this configuration with SQL Server and MVC3.

    Web.config:

    <system.web>
    <roleManager enabled="true" defaultProvider="SqlRoleManager">
      <providers>
        <clear />
        <add name="SqlRoleManager" type="System.Web.Security.SqlRoleProvider"   connectionStringName="SqlRoleManagerConnection" applicationName="YourAppName" />
      </providers>
    </roleManager>
    

    ....

    <authentication mode="Windows" />
    

    ....

    <connectionStrings>
    
    <add name="SqlRoleManagerConnection" connectionString="Data Source=YourDBServer;Initial Catalog=AppServices;Integrated Security=True;" providerName=".NET Framework Data Provider for OLE DB" />
    </connectionStrings>
    

    To inicialize roles:

    Global.asax.cs

    using System.Web.Security;
    
    ////
    protected void Application_Start()
    {
    
       //You could run this code one time and then manage the rest in your application.
       // For example:
    
       // Roles.CreateRole("Administrator");    
       // Roles.AddUserToRole("YourDomain\\AdminUser", "Administrator");
    
    
       Roles.CreateRole("CustomRole");   
    
       Roles.AddUserToRole("YourDomain\\DomainUser", "CustomRole");
    
     }
    

    In your Controller

    [Authorize(Roles ="CustomRole")]
    public class HomeController : Controller
     {
    

    To manage users

     public class Usuario
    {
        public string UserName { get; set; }
        public string RoleName { get; set; }
        public string Name { get; set; }
        public const string Domain = "YourDomain\\";
    
    
        public void Delete()
        {
            Roles.RemoveUserFromRole(this.UserName, this.RoleName);
        }
    
        public void Save()
        {
            if (Roles.IsUserInRole(Usuario.Domain + this.UserName, this.RoleName) == false)
            {
                Roles.AddUserToRole(Usuario.Domain + this.UserName, this.RoleName);
            }
        }
    }
    

    Users Class

    public class Usuarios : List<Usuario>
    {
    
        public void GetUsuarios() //Get application's users
        {
    
            if (Roles.RoleExists("CustomRole"))
            {
                foreach (string _usuario in Roles.GetUsersInRole("CustomRole"))
                {
                    var usuario = new Usuario();
                    usuario.UserName = _usuario;
                    usuario.RoleName = "CustomRole";
                    this.Add(usuario);
                }
            }
      //
    
      public void GetUsuariosRed() //Get Network Users (AD)
        {
            var domainContext = new PrincipalContext(ContextType.Domain);
            var groupPrincipal = GroupPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, "Domain Users");
    
            foreach (var item in groupPrincipal.Members)
            {
                var usuario = new Usuario();
                usuario.UserName = item.SamAccountName;
                usuario.Name = item.Name;
                this.Add(usuario);
            }
    
        }
    

    You can create an "Admin" controller like this, to manage the users:

    [Authorize(Roles = "AdminCustomRole")]
    public class AdminController : Controller
    {
    
    //
    
    public ActionResult Index()
        {
    
            var Usuarios = new Usuarios();
            Usuarios.GetUsuarios();
            return View(Usuarios);
    
        }
    
    [HttpGet]
    public ActionResult CreateUser()
        {
    
            var Usuarios = new Usuarios();
            Usuarios.GetUsuariosRed();
    
           return View(Usuarios);
    
        }
    
    //
    

    In my application, custom roles are fixed.

    0 讨论(0)
提交回复
热议问题