【问题标题】:best practice or a standard way of managing my authenticated user data with claims data使用索赔数据管理经过身份验证的用户数据的最佳实践或标准方法
【发布时间】:2020-02-08 05:53:47
【问题描述】:

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

场景:用户使用第 3 方登录并被重定向回我的应用程序。此时我所拥有的只是一个映射到我们数据库中用户配置文件的 ID。 Angular 应用程序将向后端 api 发出请求。

所以我想知道将用户在我们数据库中的角色和标志放在哪里,以限制路由访问和其他事情。

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

所以我认为我有几个选择:将我的数据添加到会话、将我的数据添加到当前用户 (ClaimsPrincipal) 或使用缓存。这些都有取舍。会话锁,缓存也有同步问题,获取每个请求都有延迟,ClaimsPrincipal 的东西似乎不守规矩?

我使用网络框架 4.7

链接到代码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】:

我最终将 Owin 与应用程序 Cookie 和声明一起使用。我能够轻松地将这些转换为客户端状态的 cookie 并将它们用于权限。

这里的答案很好

Is claims based authorization appropriate for individual resources

【讨论】:

    猜你喜欢
    • 2015-05-23
    • 2017-07-05
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多