【问题标题】:How to Implement Active Directory Authentication in ASP.NET Web API Through Forms?如何通过表单在 ASP.NET Web API 中实现 Active Directory 身份验证?
【发布时间】:2015-03-25 09:43:21
【问题描述】:

我已经阅读了很多指南/文章,但还没有找到完全符合我要求的...即通过表单在 ASP.NET Web API 中实现 Active Directory 身份验证。

类似于本指南的内容:

Cool MVC 5 guide to implement authentication with Active Directory

这非常好,但它适用于 MVC,即它使用控制器而不是 ApiController

有人可以给我关于如何开始的提示/提示/文章吗?特别是关于连接到活动目录的部分。我已经被困了一段时间了。

更新:

public bool IsAuthenticatedUser(string srvr, string usr, string password)
       {
           bool authenticated = false;

           try {
               DirectoryEntry entry = new DirectoryEntry(srvr, usr, password);
               object nativeObject = entry.NativeObject;
               Object obj = entry.NativeObject;
               authenticated = true;
           }
           catch {
               throw new HttpResponseException(HttpStatusCode.Unauthorized);
           }
           return authenticated;
       }

       // POST: api/Login
       public void Post([FromBody]string username, [FromBody]string password)
       {
           if (IsAuthenticatedUser("LDAP string", username, password))
           {
               Redirect("Index");
           }
           else
           {
               throw new HttpResponseException(HttpStatusCode.Unauthorized);
           }
       }

我正在考虑尝试这样的身份验证,你的想法?

【问题讨论】:

    标签: c# asp.net asp.net-web-api active-directory directoryservices


    【解决方案1】:

    好吧,我认为为 WebApi 进行 FORMS 身份验证是不正确的。 WebApi 的意义在于以 RESTful 的方式处理数据。

    所以我的建议是(如果你想使用 AD FORMS 身份验证):

    1) 创建测试环境来测试 AD 身份验证 - 为此,您可以使用 Oracle VirtualBox。在其上,您要安装 Windows Server 2016(评估 180 天),在其中构建 AD,创建域并添加一些测试用户,安装 AD SSL 证书(手工制作也可以);

    2) 在主机上安装来自 1) 的证书,用于主机和虚拟 PC 之间的 SSL 连接(因为您要发送纯凭据);

    3) 在您的 Web 应用程序中,您制作传统的 MVC 登录页面,使用 SSL cookie 来存储凭据信息:您创建此 cookie在您的 Authenticate 控制器方法中。认证过程很简单,在web.config中为System.Web.Security.ActiveDirectoryMembershipProvider编写正确的连接字符串,检查用户合法性是一个普通的Membership。 ValidateUser 方法;

    4) 成功验证用户后,使用保存的 cookie 在内部 WebApi 请求之间验证用户

    【讨论】:

      猜你喜欢
      • 2013-04-25
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多