获取asp.net 5中的用户组

时间:2016-04-22 22:29:31

标签: asp.net-core-mvc

我正在尝试为特定用户获取所有组。我可以在.net 4.6.1中使用以下代码。它为我提供了传递用户的组。

using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
namespace ADAuthentication.AD
{
    public class ADHelper
    {
        public IList<GroupPrincipal> GetUserGroups(string userName)
        {
            var allGroupsOftheUser = new List<GroupPrincipal>();

            PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

            UserPrincipal user = UserPrincipal.FindByIdentity(ctx, userName);
            if (user != null)
            {
                var ADgroups = user.GetAuthorizationGroups();

                foreach (GroupPrincipal groupName in ADgroups)
                {
                    allGroupsOftheUser.Add(groupName);
                }
            }

            return allGroupsOftheUser;
        }
    }
}

但在Asp.Net 5中它说,

Error   CS0234  The type or namespace name 'DirectoryServices' does not exist in the namespace 'System' (are you missing an assembly reference?)    ADAuthentication.DNX Core 5.0   

我的项目有以下project.json

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.ApplicationInsights.AspNet": "1.0.0-rc1",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions" : "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel",
  },

  "frameworks": {
    "dnx451": {
      "frameworkAssemblies": {
        "System.DirectoryServices.AccountManagement": "4.0.0.0"
      }
    },
    "dnxcore50": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ]
}

0 个答案:

没有答案
相关问题