我使用一个用户名/密码列表和一个简单的web表单,允许用户输入他们的用户名/密码的数据库。
当他们提交页面时,我只需执行存储过程检查以进行身份验证。如果他们被授权,那么他们的用户详细信息(例如用户名、道布、地址、公司地址、其他重要信息)将存储在自定义用户对象中,然后存储在会话中。我创建的这个自定义用户对象将在整个web应用程序中使用,也可以用于子站点(会话共享)中。
我的问题是:
非常感谢您的建议和回答。
我
发布于 2010-05-27 18:48:51
在使用这样一个简单的场景时,
InProc
会话状态。而是将会话存储到数据库(SqlSessionState
)或使用StateServer
。然后,应用程序池可以循环使用一整天,而不会干扰您的会话。将会话超时设置为60分钟左右,就可以解决剩余的问题。永远不要使用无session.SqlSessionState
中的自定义系统,这都无关紧要。成员资格提供程序会将配置文件ID存储到cookie中,就像会话状态会将会话ID保存到cookie中一样。发布于 2010-05-27 18:39:24
您可以使用ASP.NET Membership, Roles, Forms Authentication, and Security Resources,我将给出一个使用c#的示例
参考Forms Authentication in ASP.NET 2.0
//code for checking user name & password
protected void btnlogin_Click(object sender, EventArgs e)
{
try
{
if (txtUserName.Text.Trim() != "" && txtPassword.Text.Trim() != "")
{
User obj = objUser.UserAuthenticate(txtUserName.Text.Trim(), txtPassword.Text.Trim());
if (obj != null)
{
//To set AuthenticationCookie of usertype "User"
SetAuthenticationCookie("User", obj.UserID.ToString(), obj.DisplayName);
HttpCookie usercookie = new HttpCookie("LoginTime");
usercookie.Value = DateTime.Now.ToString();
Response.Cookies.Add(usercookie);
HttpCookie namecookie = new HttpCookie("LoginName");
namecookie.Value = obj.DisplayName;
Response.Cookies.Add(namecookie);
}
else
{
lblMsg.Text = "Invalid Username or Password.";
}
}
else
{
//lblMsg.Visible = true;
}
}
catch (Exception ex)
{
//lblMsg.Visible = true;
throw ex;
}
}
private void SetAuthenticationCookie(string role, string userid, string name)
{
string userdata = "logintype=user|role=" + role + "|email=" + txtUserName.Text.Trim() + "|name=" + name;
FormsAuthenticationTicket faTicket = new FormsAuthenticationTicket(1, userid,
DateTime.Now, DateTime.Now.AddHours(+1),
false, userdata);
HttpCookie authCookie = new HttpCookie( FormsAuthentication.FormsCookieName,//"martinfernandez@ispg.in",
FormsAuthentication.Encrypt(faTicket));
authCookie.Expires = faTicket.Expiration;
Response.Cookies.Add(authCookie);
}
//code inside global.asax.cs
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (Context.User != null && Context.User.Identity is FormsIdentity && Context.User.Identity.IsAuthenticated)
{
FormsAuthenticationTicket faTicket = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value);
string[] userdata = faTicket.UserData.Split("|".ToCharArray());
string logintype = "";
string email = "";
string uname = "";
string roleString = "";
foreach (string s in userdata)
{
string keyname = s.Split("=".ToCharArray())[0];
if (keyname == "logintype")
{
logintype = s.Split("=".ToCharArray())[1];
}
else if (keyname == "role")
{
roleString = s.Split("=".ToCharArray())[1];
}
}
string[] rolesArray = roleString.Split(";".ToCharArray());
Context.User = new System.Security.Principal.GenericPrincipal(new FormsIdentity(faTicket), rolesArray);
}
}
发布于 2010-05-28 14:29:50
顺便说一下第四点--如果不在内存中存储每个人的"User“对象,并在每次HTTP请求时重新创建对象,在内存方面会更有效率。这样,您还可以重新验证登录详细信息--如果某人的帐户被攻破,并且实际用户更改了他们的密码以尝试保护他们的帐户,但“坏用户”已经登录了,该怎么办?在您的安全机制下,“坏用户”可以继续浏览,因为用户数据被缓存,并且不会在每次回发时重新验证。
https://stackoverflow.com/questions/2920175
复制相似问题