窗口安全性验证机制设置无效,困扰多日,百分求助,谢谢各位。
日月星微 2005-11-16 09:17:21 Web.config
<authentication mode="Forms">
<forms loginUrl="index.aspx"/>
</authentication>
登录按钮单击事件
private void BTLogin_Click(object sender, System.EventArgs e)
{
......
//用户名和密码验证
int nErrorType = CValidate.ValidateAdmin(TBAdminLoginName.Text.Trim(),TBAdminPassword.Text.Trim());
......
//身份验证成功
UserInfo.AdminName = TBAdminLoginName.Text.Trim();
SetLoginCookie(UserInfo.AdminName,Convert.ToInt32(DDLCookieDays.SelectedValue));
// CUserCookie UserCookie = new CUserCookie(UserInfo);
Response.Redirect("Admin.aspx");
}
设置Cookie
private void SetLoginCookie(string userName, int cookieDays)
{
if (cookieDays == 0)
{
FormsAuthentication.SetAuthCookie(userName, false);
}
else
{
FormsAuthentication.SetAuthCookie(userName, true);
HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName].Expires = DateTime.Now.AddDays(cookieDays);
}
//获取初始请求路径Url
string redirectUrl = FormsAuthentication.GetRedirectUrl(userName,true).ToLower();
if (redirectUrl.IndexOf("index.aspx") == -1)
Context.Response.Redirect(redirectUrl);
else
Context.Response.Redirect("index.aspx");
}
创建Cookie
public class CUserCookie
{
private HttpCookie UserCookie;
private string CookieName = "Apparel-CookieName";
private HttpContext Context = HttpContext.Current;
private CUserInfo User;
public CUserCookie(CUserInfo user)
{
CookieName = CookieName + "-" + user.AdminName;
UserCookie = Context.Request.Cookies[CookieName];
this.User = user;
if (UserCookie == null)
{
UserCookie = new HttpCookie(CookieName);
}
else
{
UserCookie = Context.Request.Cookies[CookieName];
}
}
}
假如不登录直接请求Admin.aspx页面
在加载事件中如下写:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(HttpContext.Current.Request.IsAuthenticated == false)
{
string redirectUrl = FormsAuthentication.GetRedirectUrl(adminName,persist).ToLower();
if (redirectUrl.IndexOf("index.aspx") == -1)
HttpContext.Current.Response.Redirect(redirectUrl);
}
}
问题:
1:调用FormsAuthentication.SetAuthCookie()是应该自动创建身份验证票,并添加到Cookie的输出响应集合中,所以应该不用调用CUserCookie来创建;
2:如何在各个页面Http请求中检查是否已通过身份验证,用户是否具有身份验证票,而在向上述直接请求Admin.aspx发生时程序判断无效;
3:如何获取和登录用户名绑定的身份验证票,即已经存入Cookie输出响应集合中的状态,来判断当前用户没有身份验证票;
4:调试程序中的 FormsAuthentication.FormsCookieName 不是正确设置的CookieName,而是本地机器上的根目录下的网站名称,不得其解;
5:我想最主要的问题是对窗口身份验证的编程思路没有理顺,不知道如何设置,在哪儿设置。
6:查看他人已经设置好的Cookie机制时,发现每个页面请求都没有身份检查,不知道Asp.Net是不是在Web.config中配置好了后,以后每个请求都会自动检查。、
本人初学.NET,思路不太清晰,手头资料有限,真的困扰多日了,还往各位大侠一解多日之愁,拜谢拜谢。