最佳实践或使用声明数据管理经过身份验证的用户数据的标准方法

问题描述 投票:0回答:1

我想知道是否存在最佳实践或使用索赔数据管理用户数据的标准方法。

场景:用户使用第三方登录,然后重定向回我的应用程序。此时,我所拥有的只是一个ID,该ID映射到我们数据库中的用户个人资料。一个有角度的应用程序将向后端api发出请求。

因此,我想知道用户在我们的数据库中放置(让我们说)角色和标志的位置,以限制路由访问和其他操作。

我想我可以获取每个请求,但我可以看到它增加了一些开销。

所以我想我有几个选择:将我的数据添加到会话中,将我的数据添加到当前用户(ClaimsPrincipal)或使用缓存。这些都有权衡。会话锁定,缓存也存在同步问题,获取每个请求都有延迟,ClaimsPrincipal内容似乎不规则?

使用Net Framework 4.7的im

链接到代码https://github.com/ricardosaracino/SamlNet


public class UserHandler : DelegatingHandler
    {
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
            CancellationToken cancellationToken)
        {           
            var currentUser = HttpContext.Current.User as ClaimsPrincipal;

            var claims = currentUser?.Claims;
            var nameIdentifierClaim = claims?.FirstOrDefault(claim => claim.Type == ClaimTypes.NameIdentifier);
            var nameId = nameIdentifierClaim?.Value;

            // if NOT cached or expired 
            // read from database
            // set user in cache with nameid

            // set request.Properties from cache
            request.Properties["currentUser"] = new CurrentUser()
            {
                Id = Guid.NewGuid()
            };

            return base.SendAsync(request, cancellationToken).ContinueWith((task) =>
            {
                var a = request.Properties["currentUser"];

                // if modified add back to cache

                return task.Result;
            });
        }
    }

c# asp.net asp.net-mvc claims-based-identity
1个回答
0
投票

我最终将Owin与Application Cookie和Claims一起使用。我能够轻松地将它们转换为cookie以获取客户端状态,并将其用于权限。

这里很好的答案

Is claims based authorization appropriate for individual resources

© www.soinside.com 2019 - 2024. All rights reserved.