【发布时间】:2010-11-11 19:47:37
【问题描述】:
出于我不想讨论的原因,我需要为我的应用创建一个自定义身份验证系统。我只是在查看系统,并且怀疑我的解决方案是否是线程安全的。我的目标是创建一个解决方案,允许我的应用程序一次对用户进行身份验证,并且用户身份验证信息将由所有使用的母版页、页面、类、用户控件等共享。 (但不会在用户之间共享相同的信息)
这是我的设置:
PageHttpModule.cs - 作为 httpModule 添加到 web.config。
public class PageHttpModule : IHttpModule
{
public void Init(HttpApplication app)
{
app.AuthenticateRequest += new EventHandler(OnAuthenticateRequest);
}
public void OnAuthenticateRequest(Object s, EventArgs e)
{
CurrentUser.Initialize();
}
public void Dispose() { }
}
CurrentUser.cs
public static class CurrentUser
{
public static bool IsAuthenticated { get; private set; }
public static string Email {get; set;}
public static string RealName {get; set;
public static string UserId {get; set;}
public static void Initialize()
{
CurrentUser.AuthenticateUser();
}
Note: this is a scaled down version of my authentication code.
public static void AuthenticateUser()
{
UserAuthentication user = new UserAuthentication();
user.AuthenticateUser();
if (user.IsAuthenticated)
{
CurrentUser.IsAuthenticated = true;
CurrentUser.UserId = user.UserId;
CurrentUser.Email = user.Email;
CurrentUser.RealName = user.RealName;
}
}
}
UserAuthentication.cs
public class UserAuthentication
{
public string Email { get; set; }
public string RealName { get; set; }
public string UserId { get; set; }
public bool IsAuthenticated { get; private set; }
public UserAuthentication()
{
IsAuthenticated = false;
Email = String.Empty;
RealName = String.Empty;
UserId = String.Empty;
}
public void AuthenticateUser()
{
//do some logic here.. if the user is ok then
IsAuthenticated = true
Email = address from db
UserId = userid from db;
Realname = name from db;
}
}
我已经在 3 种不同的浏览器之间进行了测试,似乎运行良好,但我仍在学习,不想犯大错。
如果我的逻辑完全错误,那我应该怎么做,这样我就不必直接在每个页面上进行用户查找?
【问题讨论】:
-
我喜欢以“我要做这件完全疯狂的事情而且我不会讨论为什么”开头的问题。
-
我在经典 ASP 中有一个现有的身份验证方案,我需要镜像... :)
标签: c# .net asp.net static class-design