繁体   English   中英

如何创建用于身份验证的 Windows 服务,自定义 Web 应用程序可使用该服务对 AD 用户进行身份验证?

[英]How do I create a windows service for authentication that custom web applications can use to authenticate AD users?

我的公司正在转向更集中的模型,我们希望有一个服务来检查登录系统的用户再次在 AD 中进行分组。 该服务应该像一个插件,因为当我们创建或更新应用程序时,我们应该能够将服务添加到应用程序中,几乎不需要配置。 我对 Active Directory 有一些经验,但我只需要找出启​​动项目的最佳方式。

在您的问题中,您已经回答说这是一个 MVC 应用程序。 你可以这样做:

[Authorize(Role = "role 1, role2")]
public ActionResult Index()
{
  //your code here
  return //your return object 
}

这是我创建的一个类文件,它将为您和更多的人做检查,并且应该做您需要的一切。

public class IsUserInRole
{

    public bool IsInGroup(string groupName)
    {
        var myIdentity = GetUserIdWithDomain();
        var myPrincipal = new WindowsPrincipal(myIdentity);
        return myPrincipal.IsInRole(groupName);
    }

    public  WindowsIdentity GetUserIdWithDomain()
    {
        var myIdentity = WindowsIdentity.GetCurrent();
        return myIdentity;
    }

    public string GetUserId()
    {
        return GetUserInformation().Name;
    }

    public string GetUserDisplayName()
    {
        return GetUserInformation().DisplayName;

    }

    public UserPrincipal GetUserInformation()
    {
        var id = GetUserIdWithDomain().Name.Split('\\');

        var dc = new PrincipalContext(ContextType.Domain, id[0]);
        return UserPrincipal.FindByIdentity(dc, id[1]);
    }

    public UserPrincipal GetUserInformation(string domain, string lanId)
    {
        var dc = new PrincipalContext(ContextType.Domain, domain);
        return UserPrincipal.FindByIdentity(dc, lanId);
    }


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM