小编Gab*_*oya的帖子

使用OWIN和ASP.NET WEB API的基本认证中间件

我创建了一个ASP.NET WEB API 2.2项目.我使用基于Windows Identity Foundation的模板为visual studio中提供的个人帐户在此处查看.

Web客户端(用angularJS编写)使用带有Web浏览器cookie的OAUTH实现来存储令牌和刷新令牌.我们受益于有用的UserManagerRoleManager类,用于管理用户及其角色.使用OAUTH和Web浏览器客户端一切正常.

但是,对于基于桌面的客户端的一些复古兼容性问题,我还需要支持基本身份验证.理想情况下,我希望[授权],[授权(角色="管理员")]等属性与OAUTH和基本身份验证方案一起使用.

因此,遵循LeastPrivilege的代码,我创建了一个继承自AuthenticationMiddleware的OWIN BasicAuthenticationMiddleware.我来到以下实现.对于BasicAuthenticationMiddleWare,与Leastprivilege的代码相比,只有Handler发生了变化.实际上我们使用ClaimsIdentity而不是一系列Claim.

 class BasicAuthenticationHandler: AuthenticationHandler<BasicAuthenticationOptions>
{
    private readonly string _challenge;

    public BasicAuthenticationHandler(BasicAuthenticationOptions options)
    {
        _challenge = "Basic realm=" + options.Realm;
    }

    protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
    {
        var authzValue = Request.Headers.Get("Authorization");
        if (string.IsNullOrEmpty(authzValue) || !authzValue.StartsWith("Basic ", StringComparison.OrdinalIgnoreCase))
        {
            return null;
        }

        var token = authzValue.Substring("Basic ".Length).Trim();
        var claimsIdentity = await TryGetPrincipalFromBasicCredentials(token, Options.CredentialValidationFunction); …
Run Code Online (Sandbox Code Playgroud)

claims-based-identity wif owin asp.net-web-api2 asp.net-identity-2

11
推荐指数
1
解决办法
8867
查看次数

在惰性索引集合上使用自动 ID 生成功能创建文档时出现冲突错误 (409)

我在一个集合上使用 DocumentDB 和.NET SDK,并使用 .NET 为该集合设置了自定义索引策略IndexingMode.Lazy。这为我提供了集合上所有操作的最终一致性。

我想对非关键数据进行类似更新插入的操作:我可以承受重复和错过的更新。

我使用这样的代码:

public async Task UpsertChunk(MyChunk chunk)
{
    var id = _documentClient
                    .CreateDocumentQuery<PersistentChunk>()
                    .Where(c => c.ChunkKey == chunk.ChunkKey)
                    .Select(c => c.id)
                    .FirstOrDefault();

    var persistentChunk = chunk.ToPersistentChunk();
    if (string.IsNullOrEmpty(id))
    {
        await _documentClient.CreateDocumentAsync(_collectionUri, persistentChunk);
    }
    else
    {
        persistentChunk.id = id;
        var uri = UriFactory.CreateDocumentUri(_databaseId, _collectionId, id);
        await _documentClient.ReplaceDocumentAsync(uri, persistentChunk);
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到了不一致的冲突错误Microsoft.Azure.Documents.DocumentClientException: Message: {"Errors":["Resource with specified id or name already exists"]}

但是,由于我使用自动 ID 生成,因此即使在并发写入的情况下也不应该有重复的 …

azure-cosmosdb

5
推荐指数
1
解决办法
7585
查看次数