OAuth:ASP.NET Web API User.Identity 不会加载身份验证令牌提供程序设置的声明



我正在使用OAuth持有者身份验证,在Startup.cs中配置如下:

        OAuthBearerAuthenticationOptions oAuthBearerOptions = 
            new OAuthBearerAuthenticationOptions
            {
                AccessTokenProvider = new AccessTokenProvider(),
                AuthenticationMode = AuthenticationMode.Active
            };
        app.UseOAuthBearerAuthentication(oAuthBearerOptions);

。其中 AccessTokenProvider 实现为:

public class AccessTokenProvider : AuthenticationTokenProvider
{
    public override async Task ReceiveAsync(AuthenticationTokenReceiveContext context)
    {
        // Internal logic to get data needed for building identity...
        // Create claims identity
        ClaimsIdentity identity = new ClaimsIdentity(identityName);
        identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, nameIdentifier));
        // Add other claims
        // Set claims identity
        context.SetTicket(new AuthenticationTicket(identity, new AuthenticationProperties()));
    }
}

如果我在 ReceiveAsync 末尾设置断点,我可以验证标识是否正确构建(具有声明)以及是否达到SetTicket

但是当我尝试从 Web API 控制器访问标识时:

public abstract class BaseStorageController : ApiController
{
    protected IStorageService StorageService;
    protected BaseStorageController(IStorageServiceFactory storageServiceFactory)
    {
        StorageService = storageServiceFactory.CreateStorageService(User.Identity as ClaimsIdentity);
    }

}

。标识上的声明列表为空!

这是什么原因造成的?

旁注:我不知道这是否相关,但我正在使用温莎城堡作为 IOC 容器将依赖项注入我的控制器(在上述情况下为 IStorageServiceFactory)。在我添加之前,上述内容似乎有效(声明不是空的)。但是,我没有使用 CW 来管理与身份验证相关的任何内容。这是我的 API 控制器的 CW 安装程序:

public class ApiControllerInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Classes.FromThisAssembly().BasedOn<ApiController>().LifestylePerWebRequest());
    }
}

我找到了答案。它与依赖注入/控制反转无关。我不确定在添加之前我认为它是如何工作的。

该问题类似于此处描述的内容(但在我的情况下,解决方案是不同的):用户(IPrincipal)在使用Web Api 2.1和Owin的ApiController构造函数上不可用

基本上IPrincipal无法从 api 控制器的构造函数访问,这就是为什么没有声明(用户尚未通过身份验证)的原因。 只能从控制器的操作访问User.Identity,而不是从构造函数访问。我将基本控制器实现更改为以下内容以解决此问题:

public abstract class BaseStorageController : ApiController
{
    private readonly IStorageServiceFactory _storageServiceFactory;
    private IStorageService _storageService;
    protected BaseStorageController(IStorageServiceFactory storageServiceFactory)
    {
        _storageServiceFactory = storageServiceFactory;
    }
    protected IStorageService StorageService
    {
        get
        {
            if (_storageService == null)
            {
                _storageService = _storageServiceFactory.CreateStorageService(User.Identity as ClaimsIdentity);
            }
            return _storageService;
        }
    }
}

由于只能从控制器操作访问StorageService,因此User.Identity经过身份验证,并在调用StorageService getter 时填充声明。

希望这对某人有所帮助!

 protected IStorageService StorageService
{
    get
    {
        if (_storageService == null)
        {
            _storageService = _storageServiceFactory.CreateStorageService(User.Identity as ClaimsIdentity);
        }
        return _storageService;
    }
}

这不是实现 DI 的最佳方法

使用构造函数注入要好得多。检查 C#/Unity 中的构造函数注入?如果您不熟悉Unity,请点击此链接,非常有用:https://msdn.microsoft.com/en-us/library/dn223671(v=pandp.30).aspx

问候

最新更新