积极答复者
验证的问题;

问题
-
forms的验证;
在配置文件中,可以设置哪些窗体可以访问,哪些需要更高一级权限的访问;
那么如果这种情况下呢?比如"/index.aspx?forums=temp&id=aa"是允许匿名访问的;而"/index.aspx?forums=bb"是不允许匿名访问的;
当从"/index.aspx?forums=temp&id=aa"转到"/index.aspx?forums=bb"时,会定向到登录页面。而这两则都是"/index.aspx"窗体页传参数生成的。
也就是原始只有"/index.aspx"这一个窗体页,在配置文件中它所对应的用户的权限是固定的;对于动态生成窗体则要编程控制,如下是我的理解:
在“/index.aspx/cs”中处理。
获取COOKIE上的票证,得到用户名,密码后和数据库验证。若同则直接跳转到"/index.aspx?forums=bb";否跳转到登录页;
假如我描述有误,请指点!!!
假如没有错,那怎么得到用户COOKIE中的信息呢?<authentication mode="Forms"> <forms name=".ASPXAUTH" loginUrl="/Default.aspx" protection="All" timeout="30" path="/" /> </authentication>
都说上面的name=".ASPXAUTH"是COOKIE的名字,但是要从COOKIE中获取用户名,密码。怎么可能呢?cookie存的不是KEY所对应的值吗?应该是一个字符串,怎么获取用户名和密码呢?若有错,请指点。
麻烦各位回答一下吧;我现在太晕了!!!
大其心,可容天下之物; 虚其心,可受天下之善;
答案
-
您好,我将验证代码的步骤整理如下,请参考:
第一步:配置web.config,在<system.web>中作如下设置
<!-- 选择验证的模式-->
<authentication mode="Forms">
<forms loginUrl="Login.aspx" name="AuthCookie" timeout="60" path="/" protection="All">
</forms>
</authentication>
<!-- 设置授权-->
<authorization>
<deny users="?"/>
<allow users="*"/>
</authorization>
第二步:在Login.aspx的登入按钮中写如下代码
protected void btn_Validate_Click(object sender, EventArgs e)
{
...... //在这里编写数据库的验证代码。如果不通过则返回错误,如果通过则进入下面代码
if(验证通过){
string roles = "";
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1, // version
txt_UserName.Text, // user name
DateTime.Now, // creation
DateTime.Now.AddMinutes(60),// Expiration
false, // Persistent
roles);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);//Encrypt方法创建一个字符串,其中包含适用于 HTTP Cookie 的加密的 Forms 身份验证票证
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); //将加密的票据加入到Cookie中,
//FormsAuthentication.FormsCookieName获取的就是配置文件中名为AuthCookie的Cookie
Response.Cookies.Add(authCookie); //加入到Cookie中,这一步很重要,加入后代表已携带身份
this.Response.Redirect("/");
}
}
做完以上两步后,就可以访问受保护的页面了。- 已编辑 JiyuanModerator 2009年7月30日 10:59
- 已标记为答案 lfofiug 2009年7月30日 11:38
-
如何使用cookies? 创建一个名为page1.aspx的页面, 接着拖放一个button和TextBox到page上。双击 button并添加下面的代码: protected void Button1_Click(object sender, EventArgs e) { HttpCookie cookie = new HttpCookie("UserName"); cookie.Value = TextBox1.Text; cookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(cookie); Response.Redirect("Page2.aspx"); } page1.aspx, <div> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </div> 对于 page2.aspx, 双击form并把下面的代码放在里面: protected void Page_Load(object sender, EventArgs e) { if (Request.Cookies["UserName"] != null) Response.Write(Request.Cookies["UserName"].Value); }
jon.valett@gmail.com- 已标记为答案 lfofiug 2009年7月30日 11:38
-
protected void Button1_Click(object sender, EventArgs e)
{
//判断用户名密码是否正确
//得到用户的角色,测试时暂时写死
string userRoles = "Admins,testst";
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, user, DateTime.Now, DateTime.Now.AddMinutes(30), false, userRoles, "/");
string HashTicket = FormsAuthentication.Encrypt(Ticket);
//把角色信息保存到Cookie中去
HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket);
Response.Cookies.Add(UserCookie);
if (Context.Request["ReturnUrl"] != null)
{
Response.Redirect(Context.Request["ReturnUrl"]);
}
else
{
Response.Redirect(FormsAuthentication.DefaultUrl);
}
}
jon.valett@gmail.com- 已标记为答案 lfofiug 2009年7月30日 11:38
-
如果要和.Net提供的验证机制整合起来,还需要在Global.asax的Application_AuthenticateRequest事件中添加如下代码:
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];//获取web.config中的名为“AuthCookie”的Cookie值
if (null == authCookie)
{
// 如果没有则返回
return;
}
FormsAuthenticationTicket authTicket = null;
try
{
//解密身份票据
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch (Exception ex)
{
throw ex;
}
if (null == authTicket)
{
// 如果没有则返回
return;
}
//如果有角色可以获取传递的用户数据
//用户数据在登入代码中实例化FormsAuthenticationTicket时设置的。string userdata
string[] roles = authTicket.UserData.Split(new char[] { '|' });
//创建标识对象
System.Security.Principal.IIdentity id = new FormsIdentity(authTicket);
//创建安全主体
System.Security.Principal.IPrincipal principal = new System.Security.Principal.GenericPrincipal(id, roles);
// 添加到Http上下文中
Context.User = principal;
}
这样在受保护的页面的Page_Load事件中可以获得已验证的用户
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//是否已验证 可以把下面代码重构为一个类以方便日后使用
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
//已验证获取用户名,该用户等于上面第二步中txt_UserName.Text
string userName = HttpContext.Current.User.Identity.Name;
}
}
}
有关主体安全的概念可以参考一些资料,这个是.Net Framework提供的基于主体安全的架构- 已标记为答案 lfofiug 2009年7月30日 11:38
全部回复
-
如何使用cookies? 创建一个名为page1.aspx的页面, 接着拖放一个button和TextBox到page上。双击 button并添加下面的代码: protected void Button1_Click(object sender, EventArgs e) { HttpCookie cookie = new HttpCookie("UserName"); cookie.Value = TextBox1.Text; cookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(cookie); Response.Redirect("Page2.aspx"); } page1.aspx, <div> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </div> 对于 page2.aspx, 双击form并把下面的代码放在里面: protected void Page_Load(object sender, EventArgs e) { if (Request.Cookies["UserName"] != null) Response.Write(Request.Cookies["UserName"].Value); }
jon.valett@gmail.com- 已标记为答案 lfofiug 2009年7月30日 11:38
-
protected void Button1_Click(object sender, EventArgs e)
{
//判断用户名密码是否正确
//得到用户的角色,测试时暂时写死
string userRoles = "Admins,testst";
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, user, DateTime.Now, DateTime.Now.AddMinutes(30), false, userRoles, "/");
string HashTicket = FormsAuthentication.Encrypt(Ticket);
//把角色信息保存到Cookie中去
HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket);
Response.Cookies.Add(UserCookie);
if (Context.Request["ReturnUrl"] != null)
{
Response.Redirect(Context.Request["ReturnUrl"]);
}
else
{
Response.Redirect(FormsAuthentication.DefaultUrl);
}
}
jon.valett@gmail.com- 已标记为答案 lfofiug 2009年7月30日 11:38
-
您好,我将验证代码的步骤整理如下,请参考:
第一步:配置web.config,在<system.web>中作如下设置
<!-- 选择验证的模式-->
<authentication mode="Forms">
<forms loginUrl="Login.aspx" name="AuthCookie" timeout="60" path="/" protection="All">
</forms>
</authentication>
<!-- 设置授权-->
<authorization>
<deny users="?"/>
<allow users="*"/>
</authorization>
第二步:在Login.aspx的登入按钮中写如下代码
protected void btn_Validate_Click(object sender, EventArgs e)
{
...... //在这里编写数据库的验证代码。如果不通过则返回错误,如果通过则进入下面代码
if(验证通过){
string roles = "";
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1, // version
txt_UserName.Text, // user name
DateTime.Now, // creation
DateTime.Now.AddMinutes(60),// Expiration
false, // Persistent
roles);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);//Encrypt方法创建一个字符串,其中包含适用于 HTTP Cookie 的加密的 Forms 身份验证票证
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); //将加密的票据加入到Cookie中,
//FormsAuthentication.FormsCookieName获取的就是配置文件中名为AuthCookie的Cookie
Response.Cookies.Add(authCookie); //加入到Cookie中,这一步很重要,加入后代表已携带身份
this.Response.Redirect("/");
}
}
做完以上两步后,就可以访问受保护的页面了。- 已编辑 JiyuanModerator 2009年7月30日 10:59
- 已标记为答案 lfofiug 2009年7月30日 11:38
-
如果要和.Net提供的验证机制整合起来,还需要在Global.asax的Application_AuthenticateRequest事件中添加如下代码:
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];//获取web.config中的名为“AuthCookie”的Cookie值
if (null == authCookie)
{
// 如果没有则返回
return;
}
FormsAuthenticationTicket authTicket = null;
try
{
//解密身份票据
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch (Exception ex)
{
throw ex;
}
if (null == authTicket)
{
// 如果没有则返回
return;
}
//如果有角色可以获取传递的用户数据
//用户数据在登入代码中实例化FormsAuthenticationTicket时设置的。string userdata
string[] roles = authTicket.UserData.Split(new char[] { '|' });
//创建标识对象
System.Security.Principal.IIdentity id = new FormsIdentity(authTicket);
//创建安全主体
System.Security.Principal.IPrincipal principal = new System.Security.Principal.GenericPrincipal(id, roles);
// 添加到Http上下文中
Context.User = principal;
}
这样在受保护的页面的Page_Load事件中可以获得已验证的用户
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//是否已验证 可以把下面代码重构为一个类以方便日后使用
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
//已验证获取用户名,该用户等于上面第二步中txt_UserName.Text
string userName = HttpContext.Current.User.Identity.Name;
}
}
}
有关主体安全的概念可以参考一些资料,这个是.Net Framework提供的基于主体安全的架构- 已标记为答案 lfofiug 2009年7月30日 11:38