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