存储和asp.net成员访问遗留用户名 [英] Storing and accessing a legacy UserID in asp.net membership

查看:90
本文介绍了存储和asp.net成员访问遗留用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个传统的用户名(INT32),我想链接到asp.net成员。我已经建立了数据库连接的表,我很高兴的那部分。现在的问题是在哪里用户ID存储在Web应用程序以便在需要时方便使用。

I have a legacy UserID (int32) which I wish to link to asp.net membership. I have set up link tables on the database and I'm happy with that part. The question is where to store the UserID in the web application so that it is easily accessible when required.

我决定,最好的地方来存储它在Login控件的事件的loggedIn的的FormsAuthenticationTicket的一部分的UserData。我的第一个使这一访问尝试是提取它在我BasePage类的preINIT。这个麻烦的是,当通过用户控件需要用户名就变得混乱。

I decided that the best place to store it is in the UserData part of the FormsAuthenticationTicket in the LoggedIn event of the Login Control. My first attempt to make this accessible was to extract it in the PreInit of my BasePage class. The trouble with this is that it gets messy when the UserID is required by UserControls.

它是可以接受的,只是包装在一个静态方法或属性的实用工具类,是这样的:

Is it acceptable to just wrap it in a static method or property in a Utilities class, something like this:

    public static int UserID
    {
        get
        {
            int userID = 0;
            if (HttpContext.Current.User.Identity is FormsIdentity)
            {
                FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                FormsAuthenticationTicket ticket = id.Ticket;
                userID = Int32.Parse(ticket.UserData);
            }
            return userID;
        }
    }

这似乎是工作,但我不知道如果我在这里打破了一些不成文的规定。我presume所有这些东西是在内存中,以便有该访问没有很大的开销。

This seems to work but I don't know if I'm breaking some unwritten rule here. I presume all this stuff is in memory so there's no great overhead in this access.

推荐答案

您code相从功能的角度罚款(虽然我清理了一下,但是这更多的是风格的东西)。

Your code looks fine from a functional perspective (though I would clean it up a bit, but that's more a style thing).

您可能会考虑将它的扩展方法,虽然,而不是仅仅坚持在一个随机工具类。也许对于的IIdentity 类的扩展?

You might consider making it an extension method, though, rather than just sticking it in a random utility class. Maybe an extension for the IIdentity class?

int myUserId = HttpContext.Current.User.Identity.MyUserId();

使用的UserData 字段是好的,我猜。另一种选择是使用自定义的票务创建自己的的IIdentity 对象,并在包装​​他们的GenericPrincipal - 可能是你追求什么,但太辛苦了。

Using the UserData field is fine, I guess. Another option is to create your own IIdentity object with a custom Ticket and wrap them in a GenericPrincipal -- might be too much work for what you're after, though.

这篇关于存储和asp.net成员访问遗留用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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