簡體   English   中英

使用ASP.NET身份驗證和LDAP的身份驗證/授權

[英]Authentication/Authorization using ASP.NET authentication and LDAP

我有一個現有的ASP.NET應用程序,它使用LDAP進行身份驗證,並使用ASP.NET成員身份進行身份驗證和授權。

因此,LDAP用戶可以選擇使用其LDAP憑據或ASP.NET成員身份憑據進行身份驗證。 非LDAP用戶只能使用LDAP憑據進行身份驗證。

我現在想創建一個Web API項目,該項目使用類似的身份驗證和授權方法。

使用VS 2013,我創建了一個新的Web API項目,該項目使用“個人帳戶”選項進行身份驗證。

我已經在Providers \\ ApplicationOAuthProvider.cs文件中修改了GrantResourceOwnerCredentials方法。

之前

...
IdentityUser user = await userManager.FindAsync(context.UserName, context.Password);

if (user == null)
{
    context.SetError("invalid_grant", "The user name or password is incorrect.");
    return;
}
...

...
IdentityUser user;

if (AuthenticateActiveDirectory(context.UserName, context.Password, "MyADDomain"))
{
    user = await userManager.FindByNameAsync(context.UserName);
}
else
{
    user = await userManager.FindAsync(context.UserName, context.Password);
}

if (user == null)
{
    context.SetError("invalid_grant", "The user name or password is incorrect.");
    return;
}
...

AuthenticateActiveDirectory方法是:

   private bool AuthenticateActiveDirectory(string userName, string password, string domain)
   {
     bool validation;
     try
     {
        var lcon = new LdapConnection(new LdapDirectoryIdentifier((string)null, false, false));
        var nc = new NetworkCredential(userName, password, domain);
        lcon.Credential = nc;
        lcon.AuthType = AuthType.Negotiate;
        lcon.Bind(nc);
        validation = true;
     }
     catch (LdapException)
     {
        validation = false;
     }
     return validation;
   }

這行得通,但這是最好的方法還是有更好的方法?

沒關系。 幾件事想到:

  • 如果您的目錄林中有多個域(或曾經支持),則用戶名在整個目錄林中不是唯一的-只是域。 看來您今天要輸入使用者名稱。
  • 對於您的LdapConnection ,我認為空服務器名稱僅用於此處顯示? 您可以使用S.DS.AD命名空間為您找到一個域控制器,而不是對其進行硬編碼。
  • 您可能需要AuthType.Basic而不是在此處進行協商。 您將要確保您的連接是LDAP / S,因為憑據將是明文形式的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM