c# - Base Controller ASP.NET MVC 3 中的这个 Custom Principal 是不是非常低效?

标签 c# performance asp.net-mvc-3 authentication iprincipal

尽管我已经在这里待了一段时间,但这是我关于 SO 的第一个问题,所以请多多包涵。

我正在使用 ASP.NET MVC 3 并且我想创建一个自定义的 Principal 这样我就可以存储比标准更多的关于当前用户的信息因此不必经常去数据库。这是我追求的相当标准的东西。我们先说电子邮件地址和用户 ID。

我决定将对象存储在缓存中,因为我知道不建议将其存储在 session 中。

我也不想一直转换 User 对象,所以我想覆盖 Controller 中的 User 对象。所以我可以直接访问 User.UserId 并得到一些保证。

所以我创建了一个这样的自定义主体:

public class MyPrincipal : IPrincipal
{
    public MyPrincipal(IIdentity ident, List<string> roles, string email, Guid userId)
    {
        this._identity = ident;
        this._roles = roles;
        this._email = email;
        this._userId = userId;
    }

    IIdentity _identity;

    public IIdentity Identity
    {
        get { return _identity; }
    }

    private List<string> _roles;

    public bool IsInRole(string role)
    {
        return _roles.Contains(role);
    }

    private string _email;

    public string Email
    {
        get { return _email; }
    }

    private Guid _userId;

    public Guid UserId
    {
        get { return _userId; }
    }
}

我有一个这样的基础 Controller :

public class BaseController : Controller
    {
        protected virtual new MyPrincipal User
        {
            get
            {
                if (base.User is MyPrincipal)
                {
                    return base.User as MyPrincipal;
                }
                else
                {
                    return new MyPrincipal(base.User.Identity, new List<string>(0), "", Guid.Empty );
                }
            }
        }

        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (User != null)
            {
                if (User.Identity.IsAuthenticated)
                {
                    if (User.Identity is FormsIdentity)
                    {
                        FormsIdentity id = base.User.Identity as FormsIdentity;
                        MyPrincipal principal = (MyPrincipal)filterContext.HttpContext.Cache.Get(id.Name);
                        if (principal == null)
                        {
                            MembershipUser user = Membership.GetUser();

                            // Create and populate your Principal object with the needed data and Roles.
                            principal = new MyPrincipal(id, Roles.GetRolesForUser(id.Name).ToList(), user.Email, (Guid)user.ProviderUserKey);
                            filterContext.HttpContext.Cache.Add(
                            id.Name,
                            principal,
                            null,
                            System.Web.Caching.Cache.NoAbsoluteExpiration,
                            new System.TimeSpan(0, 30, 0),
                            System.Web.Caching.CacheItemPriority.Default,
                            null);
                        }
                        filterContext.HttpContext.User = principal;
                        System.Threading.Thread.CurrentPrincipal = principal;
                        base.OnAuthorization(filterContext);
                    }
                }
            }
        }
    }

如果您看一下,您很快就会意识到,如果用户尚未登录,那么对 User 对象的任何调用都必须运行这段代码:

return new MyPrincipal(base.User.Identity, new List<string>(0), "", Guid.Empty );

这对我来说感觉非常低效,尽管它只是为丢失的东西创建空对象。

它工作正常。

所以我想我想知道这是否真的可以,我应该停止对性能和效率如此苛刻,或者如果我的担心是正确的,在这种情况下我应该怎么做? [请不要说“得到生活,伙计!”]

最佳答案

没有——从性能的角度来看,这段代码没有什么特别的错误。在 ASP.NET 的后端创建了大量的对象,您的单个对象只是九牛一毛。由于类实例化非常快,所以我不会担心它。

你为什么忽略这里的 session ? session 信息没有到期日期,因此没有额外的幕后检查。除非您使用进程外 session 服务器,否则您的对象没有序列化(也没有缓存)。 缓存适用于每个用户 - 因此您有可能(尽管很小)代码错误返回错误的主体,其中缓存是每个用户 - 不会冒这种风险。

如果您希望它可用于那里的所有请求(不仅仅是基于 MVC),我会考虑在 Application_PostAuthenticateRequest 中设置它

关于c# - Base Controller ASP.NET MVC 3 中的这个 Custom Principal 是不是非常低效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8263845/

相关文章:

java - Map/Reduce wall-time 对 Reduce 任务的数量不敏感

android - jQuery 和 MVC3 Razor 在 Android 浏览器上生成奇怪的 URL

multithreading - C#.Net MVC线程中的共享变量

c# - EventHandler 应该始终用于事件吗?

c# - TagHelper ModelExpression 元数据中不存在描述属性文本

javascript - 如何在javascript之前加载图像

python - 如何加速python中多个数组的计算

c# - 使用 Membership.GetUser() 时发生错误

c# - 地形地面,城市中的草地和雪地 : Skylines

c# - 在 Unity 中使用 Stockfish Chess AI