[英]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.