Owin OAuth Provider,接受Active Directory凭据和基本凭据吗?

时间:2016-01-06 18:54:33

标签: c# asp.net authentication oauth active-directory

对于工作项目,我们有一个名为APC的中央应用程序,它处理内部应用程序的用户管理;它允许对用户,应用程序,权限和角色进行标准的CRUD操作。

此应用程序当前使用Active Directory进行身份验证,使用Windows Identity处理声明。它提供了一个DLL,其中包含我们在内部应用程序中使用的自定义授权属性。

企业希望APC能够处理非域身份验证(用户名和密码),他们也希望能够支持移动设备。为此,我们决定让我们的应用程序直接处理身份验证,并使其成为OAuth提供商;除了域登录之外,我们还会扩展用户持久性以处理用户名和密码,并提供承载令牌以支持多种应用程序类型。

通过我的研究,我发现使用OWIN创建具有基本身份验证的OAuth提供程序的好文章,例如this one。我还发现了微软的Active Directory Authentication Library (ADAL),它显然会处理OAuth部分。但是,我不确定如何在我们的提供程序中结合这两种身份验证类型。 (事实上​​,虽然我理解身份验证,但我并不熟悉.NET应用程序如何与Active Directory一起使用。通常,它在web.config中是一个标记,它只是起作用 - 我我真的不知道如何利用这条管道。)

如何使用Active Directory以用户名和密码组合作为后备进行身份验证?

这是我迄今为止所得到的,供参考。不幸的是,它并不多:

public override async Task GrantResourceOwnerCredentials( OAuthGrantResourceOwnerCredentialsContext context)
{
    context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); // TODO: Revisit.

    /* The authentication portion goes here.
    /* Check Active directory first. If that does not succeed, attempt basic authentication.
     */

    // Using Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext().AcquireToken can't work, as I need to generate my token based on the AD credentials.

    /* If AD fails, fall back to basic auth if it has been provided.
     */

    // If auth fails, set context error.
    if (false) // TODO: Revisit.
    {
        context.SetError("invalid_grant", "Authentication failed.");
    }

    // add user and app guid claims
    var identity = new ClaimsIdentity(context.Options.AuthenticationType);
    identity.AddClaim(new Claim(ApcClaimTypes.User, userGuid)); // TODO: Revisit.
    identity.AddClaim(new Claim(ApcClaimTypes.Application, appGuid)); // TODO: Revisit.

    // build claims from apc
    var apcClaims = new List<Claim>();

    // get privileges
    apcClaims.AddRange(_appService.GetPrivileges(uid, aid)
        .Select(p => new Claim(ApcClaimTypes.Privilege, p.PrivilegeCode)));

    // get roles
    apcClaims.AddRange(_appService.GetRoles(uid, aid)
        .Select(r => new Claim(ClaimTypes.Role, r.RoleCode)));

    identity.AddClaims(apcClaims);

    context.Validated(identity);
}

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作

using System.Security;
using System.DirectoryServices.AccountManagement;
public struct Credentials
{
    public string Username;
    public string Password;
}
public class Domain_Authentication
{
    public Credentials Credentials;
    public string Domain;
    public Domain_Authentication(string Username, string Password, string SDomain)
    {
        Credentials.Username = Username;
        Credentials.Password = Password;
        Domain = SDomain;
    }
    public bool IsValid()
    {
        using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, Domain))
        {
            // validate the credentials
            return pc.ValidateCredentials(Credentials.Username, Credentials.Password);
        }
    }
}

或者您可以通过简单的测试来查看用户是否也在AD中

string fullName = null;
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
    using (UserPrincipal user = UserPrincipal.FindByIdentity(context,"your domain user name"))
    {
        if (user != null)
        {
            fullName = user.DisplayName;
        }
    }
}
相关问题