C# 创建可在应用程序范围内访问的用户配置文件
我正在实现一个非常基本的登录系统,实际上并没有使用任何开箱即用的登录控件。我的数据库包含以下表: UserProfiles:包含用户的详细信息C# 创建可在应用程序范围内访问的用户配置文件,c#,asp.net,C#,Asp.net,我正在实现一个非常基本的登录系统,实际上并没有使用任何开箱即用的登录控件。我的数据库包含以下表: UserProfiles:包含用户的详细信息 UserSecurityRoles:包含每个用户的安全角色 在我的asp.net应用程序中,我使用以下代码登录用户: protected void Logon_Click(object sender, EventArgs e) { if (_BLSecurity.verifyUser(UserName.Text,
UserSecurityRoles:包含每个用户的安全角色 在我的asp.net应用程序中,我使用以下代码登录用户:
protected void Logon_Click(object sender, EventArgs e)
{
if (_BLSecurity.verifyUser(UserName.Text, UserPass.Text))
{
Response.Redirect("../Default.aspx");
}
else
{
Msg.Text = "Invalid credentials. Please try again.";
}
}
一旦用户被验证,我需要将UserProfiles和UserSecurityRoles中包含的关于他的信息存储在我的web应用程序中,这样我就可以从我拥有的每个表单访问这些数据,并根据这些数据设置权限
你知道我该怎么做吗 您可以在会话中存储UserProfilesData。每个用户都有一个与他相关联的唯一会话
protected void Logon_Click(object sender, EventArgs e)
{
if (_BLSecurity.VerifyUser(UserName.Text, UserPass.Text))
{
Session["currentUserProfile"] = GetUserProfile();
Response.Redirect("../Default.aspx");
}
else
{
Msg.Text = "Invalid credentials. Please try again.";
}
}
然后,无论何时需要UserProfile对象,都可以使用currentUserProfile键从会话中获取它
var userProfile = (UserProfile)Session["currentUserProfile"];
您可以为示例
SessionContext
创建自定义类,您将在其中保存登录时所需的所有数据。之后,您可以创建每个UI.Page继承的类。在这个类中,您将拥有SessionContext
属性
public SessionContext USession
{
get
{
if (base.Session["_USRSESS"] != null)
return (BO.SessionContext)base.Session[_USRSESS];
return null;
}
set
{
base.Session["_USRSESS"] = value;
}
}
当您需要此用户数据时,您将在每个页面中调用此属性。在这里保存一个自定义类将使您可以在会话中放入所需的所有内容,而不需要记住多个会话变量
编辑:
如果你有基础课
public class BasicPage : System.Web.UI.Page
{
public SessionContext USession
{
get
{
if (base.Session["_USRSESS"] != null)
return (BO.SessionContext)base.Session[_USRSESS];
return null;
}
set
{
base.Session["_USRSESS"] = value;
}
}
}
public partial class _Default : BasicPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
请参见aspx.pages继承BasicPage
和BasicPage
继承UI.Page
当您重定向到默认页面时,您将需要以下内容:
SessionContext resSession = new SessionContext();
// I'm giving you example with standard bool property. You can put class property if you want.
resSession.IsAdmin = Convert.ToInt32(userRow["IsAdmin"]) == 1;
// this is Guid property for the User primary key, if you are using integer for primary key the property should be int.
resSession.UserID = (Guid)userRow["ID"];
BasicPage page = this.Page as BasicPage;
page.UserSession = session;
Response.Redirect("../Default.aspx");
检查我的答案,如果有不清楚的地方,告诉我!:)谢谢,你能详细说明一下如何从父类访问会话吗?我试图使用“base.Session…”,但得到的是“object不包含Session的定义”@user3340627请稍候,我将编辑我的问题。@user3340627您可以查看完整答案。