因为签每一页上的角色Asp.net网站很慢 [英] Asp.net website very slow because of roles checkin on every page

查看:137
本文介绍了因为签每一页上的角色Asp.net网站很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的ASP.NET应用程序的角色。我有图中的问题(我认为)。这个问题在应用程序的每个页面使用的角色和权限。因此它使用以下功能在页面装载

I have roles on my ASP.NET application. I have figure the problem (I think). The problem every page in the applications uses role and permission. Therefore it uses the following function in page load

(Roles.IsUserInRole(管理))
{
//显示页面
}
其他
{
//没有
}

if (Roles.IsUserInRole("Admin")) { // display the page } else { // No }

我找到了解决我的问题,从这个问题的<一个href=\"http://stackoverflow.com/questions/1929073/poor-performance-with-windowstokenroleprovider/1929290#comment15032329_1929290\">Poor性能WindowsTokenRoleProvider

I found a solution to my problem from this question Poor Performance with WindowsTokenRoleProvider

但也有一些差异
1.上述问题,使用 WindowsTokenRoleProvider ,我使用的 SqlRoleProvider

But there are a couple of differences 1. The above question uses WindowsTokenRoleProvider, I am using SqlRoleProvider

由于上述问题,在上述溶液并不完全为我工作。

Because of the above problem, the above solution does not exactly work for me.

我迄今所做的,和我取得了部分成功,我都来源于SqlRoleProvider一类,并包括该功能,从上面的问题,但修改的同一。我改变web.config中,使它看起来像这样

What I have done so far, and I am partially successful, I have derived a class from SqlRoleProvider and include this function which is the same from above question but modified. I changed web.config so that it looks like this

<roleManager enabled="true" cacheRolesInCookie="true" cookieName=".ASPR0L3S" cookieTimeout="117" cookieSlidingExpiration="true" cookieProtection="All" createPersistentCookie="false" defaultProvider="CustomSqlRoleProvider">
            <providers>
                <add name="CustomizedRoleProvider" type="CustomSqlRoleProvider" connectionStringName="PEGConn" applicationName="/CRM"/>
            </providers>
        </roleManager>

这是我的课,它不进去的功能(只执行,当用户登录)

This is the function inside my class, which does get (executed only when a user login)

public override string[] GetRolesForUser(string username)
    {
        // Will contain the list of roles that the user is a member of
        List<string> roles = null;

        // Create unique cache key for the user
        string key = String.Concat(username, ":", base.ApplicationName);

        // Get cache for current session
        Cache cache = HttpContext.Current.Cache;

        // Obtain cached roles for the user
        if (cache[key] != null)
        {
            roles = new List<string>(cache[key] as string[]);
        }

        // Was the list of roles for the user in the cache?



        if (roles == null)
        {
            string[] AllRoles = GetAllRoles();
            roles = new List<string>();

            // For each system role, determine if the user is a member of that role
            foreach (String role in AllRoles)
            {
                if (base.IsUserInRole(username, role))
                {
                    roles.Add(role);
                }
            }

            // Cache the roles for 1 hour
            cache.Insert(key, roles.ToArray(), null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
        }

        // Return list of roles for the user
        return roles.ToArray();
    }

问题是,当Roles.IsUserInRole函数调用同样的老

The problem is when Roles.IsUserInRole function calls the same old

System.Web.Security.Roles.IsUserInRole

System.Web.Security.Roles.IsUserInRole

功能。我甚至重载这个功能在我的新类,但它永远不会被执行。我基本上缓存的所有角色,使每个页面上刷新应用程序不走,从一开始搜索的所有角色。

function. I have even overloaded this function in my new class but it never gets executed. I am basically caching all the roles so that on each page refresh the application does not go search for all roles right from the start.

我需要得到从 System.Web.Security.Roles.IsUserInRole 另一个类?有没有人这样做。

Do I need to derive another class from System.Web.Security.Roles.IsUserInRole? Has anyone done it.

每个页面需要新鲜的约4-8秒,这是太长。 code是在VS 2008,C#3.5

Each page takes about 4-8 seconds on fresh which is too long. Code is in VS 2008, C# 3.5

推荐答案

如果Roles.IsUserInRole(管理),是需要时间的,你可以检查用户的角色一次(在登录)并保存价值会话对象。

If Roles.IsUserInRole("Admin") is what takes time, you can check the role of the user once (upon logging in) and save the value is the session object.

const string IS_ADMIN_KEY; //this can be on a base class of page / master page

Session[IS_ADMIN_KEY] = Roles.IsUserInRole("Admin"); // do this when logging in

//Add this to page load
bool isAdmin = Session[IS_ADMIN_KEY]; 
if(isAdmin)) 
{ 
   // display the page 
} else 
{ 
   // don't display the page 
}

这篇关于因为签每一页上的角色Asp.net网站很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆