如何使用Windows用户登录通过Webservice进行身份验证

时间:2017-07-05 09:15:41

标签: c# .net web-services authentication active-directory

我有一个客户端 - 服务器应用程序(Windows,客户端是WPF,非UWP),我想通过活动目录进行身份验证。我的想法是从客户端所在的Windows机器上获取凭证(或令牌)并将该信息发送到服务器(通过webservice,IIS,asp.net)。然后服务器检查广告是否有效凭证并进行授权...... 所以关键点是:

  • 从client-windows提取凭据/令牌
  • 通过vebservice将其发送到服务器(该部分应该很简单)
  • 在服务器上针对活动目录进行验证

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

如果要在桌面应用程序中检查Windows用户身份验证,可以简单地使用

Environment.UserName

变量,它提供当前登录用户的用户名。 如果你想检查它是否是一个活动目录用户,你可以调用这样的函数:

public bool UserExists(string username)
{
   // create your domain context
   using (PrincipalContext domain = new PrincipalContext(ContextType.Domain))
   {
       // find the user
       UserPrincipal foundUser = UserPrincipal.FindByIdentity(domain, IdentityType.Name, username);

       return foundUser != null;
    }
}

根据您的新请求,您可以拆分上面的代码:

在客户端中,您可以使用Environment变量获取AD用户名和域名,将其传递给服务器并使用UserExist()函数检查用户是否存在