asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net – 服务器端声明缓存与Owin身份验证大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个应用程序,以前使用FormsAuthentication,一段时间,我切换使用从WindowsIdentityFramework的Identitymodel,以便我可以从基于声明的身份验证,但是它是相当丑陋的使用和实现。所以现在我在看OwinAuthentication。

我在看OwinAuthentication和Asp.Net身份框架。但是Asp.Net Identity框架的唯一实现使用Entitymodel和我使用NHibernate。所以,现在我正在寻求尝试绕过Asp.Net身份,只是直接使用Owin身份验证。我终于能够得到一个工作登录使用从“How do I ignore the Identity Framework magic and just use the OWIN auth middleware to get the claims I seek?”的提示,但现在我的cookie持有索赔是相当大。当我使用Identitymodel时,我能够使用服务器端缓存机制缓存在服务器上的声明,cookie只是为缓存的信息保存一个简单的令牌。在OwinAuthentication中有类似的功能,还是我自己实现它?

我希望我会在这些船之一…

> Cookie保持为3KB,哦,它有点大。
>启用类似于Owin中Identitymodel的SessionCaching的功能,我不知道。
>编写自己的实现来缓存导致cookie膨胀的信息,看看我是否可以在应用程序启动时配置Owin时挂断它。
>我做这个错误,有一种方法,我没有想到或我在Owin滥用的东西。

public class OwinConfiguration
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "Application",AuthenticationMode = AuthenticationMode.Active,CookiehttpOnly = true,Cookiename = "Application",ExpireTimeSpan = TimeSpan.Fromminutes(30),LoginPath = "/Login",LogoutPath = "/Logout",ReturnUrlParameter="ReturnUrl",SlidingExpiration = true,Provider = new CookieAuthenticationProvider()
            {
                OnValidatEIDentity = async context =>
                {
                    //handle custom caching here??
                }
            }
            //Cookiename = CookieAuthenticationDefaults.CookiePrefix + ExternalAuthentication.ExternalCookiename,//ExpireTimeSpan = TimeSpan.Fromminutes(5),});
    }
}@H_450_12@ 
 

更新
我能够使用Hongye提供的信息获得所需的效果,我想出了以下逻辑…

Provider = new CookieAuthenticationProvider()
{
    OnValidatEIDentity = async context =>
    {
        var userId = context.Identity.GetUserId(); //Just a simple extension method to get the ID using identity.FindFirst(x => x.Type == ClaimTypes.NamEIDentifier) and account for possible NULLs
        if (userId == null) return;
        var cacheKey = "MyApplication_Claim_Roles_" + userId.ToString();
        var cachedClaims = System.Web.httpContext.Current.Cache[cacheKey] as IEnumerable<Claim>;
        if (cachedClaims == null)
        {
            var securityservice = DependencyResolver.Current.Getservice<ISecurityservice>(); //My own service to get the user's roles from the database
            cachedClaims = securityservice.GetRoles(context.Identity.Name).SELEct(role => new Claim(ClaimTypes.Role,role.RoleName));
            System.Web.httpContext.Current.Cache[cacheKey] = cachedClaims;
        }
        context.Identity.AddClaims(cachedClaims);
    }
}@H_450_12@

解决方法

OWIN cookie身份验证中间件不支持会话缓存功能。 #2不是选项。

#3是正确的方式。正如Prabu建议的,你应该在你的代码中:

OnResponseSignIn:

>使用唯一键(GUID)在缓存中保存context.Identity
>使用唯一键创建一个新的ClaimsIdentity
>用新标识替换context.Identity

OnValidatEIDentity:

>从context.Identity获取唯一的键声明
>通过唯一键获取缓存的标识
>使用缓存的标识调用context.replacEIDentity

我会建议你gzip的cookie,但我发现OWIN已经在它的Ticketserializer。不是你的选择。

大佬总结

以上是大佬教程为你收集整理的asp.net – 服务器端声明缓存与Owin身份验证全部内容,希望文章能够帮你解决asp.net – 服务器端声明缓存与Owin身份验证所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。