还是没弄懂dotNET的身份验证,其实我的要求很简单

woolgate 2004-10-25 11:13:24
必须login,否则无法查看该webapp的任何网页,
不需要保存,身份信息从数据库得到,退出方式可以用signout按钮或者关闭ie窗口


现在看了http://support.microsoft.com/default.aspx?scid=301240 后
通过FormsAuthentication.RedirectFromLoginPage(cbxUserName.SelectedValue, false)方法通过验证,
FormsAuthentication.SignOut();Session.Abandon();Response.Redirect("default.aspx", true);退出

目前网页不多,没有登录或者退出有几个网页倒是直接定向到login.aspx了,但还是有些问题
1。单击signout按钮后某些网页还是能通过输入网址的方法访问
2。关闭ie窗口后输入某些网页的网址也能访问
这几个特殊的网页我并没有发现有什么特别的,web.config文件
<authentication mode="Forms">
<forms name="frmLogin" loginUrl="login.aspx" timeout="5">
</forms>
</authentication>

<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
怎样修改才能达到我的很简单的目的呢?
还有在程序的网页里怎样获得用户名和相关的一些信息?用Session("username"), session("canwrite")这种方法吗?
其实我觉得asp里用这种方法也挺简单的,做一个inc文件,每个网页检查session("username"),如果为空都定向到login.asp
dotNET里面怎么实现呢?
...全文
275 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
woolgate 2004-10-25
  • 打赏
  • 举报
回复
to a05(天堂之门),snowpine999([彼岸烟花])
你们的方法都是通过检查用户名密码之后把username和其他相关信息存在session中吧,然后以后每张网页最开始都检查Session("username")是否为空或者检查session("loginok")是否为1这种方法

这种方法我以前做asp时也是使用这种方法,我在首贴里最后两句提到
简单,有效,
但据说对效率影响很大,而且是一种老办法,在dotNET中应该有更有效更简便的方法吧,喝喝
噯卟釋手 2004-10-25
  • 打赏
  • 举报
回复
我们老大的登录页面 看看有没有点帮助 呵呵
噯卟釋手 2004-10-25
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace SalesPromotion
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class Login : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button ButtonLogin;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.HtmlControls.HtmlInputText TextUserCode;
protected System.Web.UI.HtmlControls.HtmlInputText TextUserPassword;



#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ButtonLogin.Click += new System.EventHandler(this.ButtonLogin_Click);

}
#endregion


private void ButtonLogin_Click(object sender, System.EventArgs e)
{
if(this.IsValid)
{
PublicDB.ClassPublicDB.AuthenticateUserEntry authUser = new PublicDB.ClassPublicDB.AuthenticateUserEntry();
authUser.AuthenticateUser(Server.HtmlDecode(this.TextUserCode.Value.Trim())
, Server.HtmlDecode(this.TextUserPassword.Value.Trim()), Session.Timeout);

if(authUser.IsAuthenticated)
{
PublicDB.ClassPublicDB.UserLog login = new SalesPromotion.PublicDB.ClassPublicDB.UserLog();
int result = login.CheckUserOnline(authUser.UserCode);
if(result != -1)
{
if((Session["LoginID"] != null) && ((int)Session["LoginID"] != -1))
{
login.UserLogout(result);
FormsAuthentication.SignOut();
Session.RemoveAll();
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, authUser.EncTicket));
Session["UserCode"] = authUser.UserCode;
Session["UserName"] = authUser.UserName;
Session["IsAuthenticated"] = true;
login.UserLogin(authUser.UserCode,"IP:"+Request.UserHostAddress);
Session["LoginID"] = login.LoginID;
Response.Redirect("Default.aspx",true);
}
else
{
Response.Write("<script>alert('有相同的用户已经登录了系统,如果是非法退出请等待5分钟后再试!');</script>");
}
}
if(result == -1)
{
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, authUser.EncTicket));
Session["UserCode"] = authUser.UserCode;
Session["UserName"] = authUser.UserName;
Session["IsAuthenticated"] = true;
login.UserLogin(authUser.UserCode,"IP:"+Request.UserHostAddress);
Session["LoginID"] = login.LoginID;
Response.Redirect("Default.aspx",true);
}
}
else
{
Response.Write("<script>alert('用户或密码错误!');</script>");
}
}
}
}
}
a05 2004-10-25
  • 打赏
  • 举报
回复
我的做法是把用户验证信息放到Session中
等注销时把Session置为空就可以了
好象没有遇到你的问题
woolgate 2004-10-25
  • 打赏
  • 举报
回复
可是注销按钮似乎并没有起到实际的作用啊
手动输入网址还是能够访问部分网页,这个问题怎么办?
forestyang 2004-10-25
  • 打赏
  • 举报
回复
//加上path = "/"
<forms name="frmLogin" loginUrl="login.aspx" timeout="5" path="/">
</forms>


//用户名可以由 HttpContext.Current.User.Identity.Name得到, 其他信息,你可以用Cookie或者Session;简单点,你可以写一个类把需要随时调用的信息包含在其中
woolgate 2004-10-25
  • 打赏
  • 举报
回复
最好同时实现如果5分钟内没有操作,自动退出
woolgate 2004-10-25
  • 打赏
  • 举报
回复
第二行的不需要保存的意思是指不需要保存身份验证信息(免登录信息),
用户退出程序或者关掉ie以后重新上的话必须重新登录
19820911 2004-10-25
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace SalesPromotion
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class Login : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button ButtonLogin;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.HtmlControls.HtmlInputText TextUserCode;
protected System.Web.UI.HtmlControls.HtmlInputText TextUserPassword;



#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ButtonLogin.Click += new System.EventHandler(this.ButtonLogin_Click);

}
#endregion


private void ButtonLogin_Click(object sender, System.EventArgs e)
{
if(this.IsValid)
{
PublicDB.ClassPublicDB.AuthenticateUserEntry authUser = new PublicDB.ClassPublicDB.AuthenticateUserEntry();
authUser.AuthenticateUser(Server.HtmlDecode(this.TextUserCode.Value.Trim())
, Server.HtmlDecode(this.TextUserPassword.Value.Trim()), Session.Timeout);

if(authUser.IsAuthenticated)
{
PublicDB.ClassPublicDB.UserLog login = new SalesPromotion.PublicDB.ClassPublicDB.UserLog();
int result = login.CheckUserOnline(authUser.UserCode);
if(result != -1)
{
if((Session["LoginID"] != null) && ((int)Session["LoginID"] != -1))
{
login.UserLogout(result);
FormsAuthentication.SignOut();
Session.RemoveAll();
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, authUser.EncTicket));
Session["UserCode"] = authUser.UserCode;
Session["UserName"] = authUser.UserName;
Session["IsAuthenticated"] = true;
login.UserLogin(authUser.UserCode,"IP:"+Request.UserHostAddress);
Session["LoginID"] = login.LoginID;
Response.Redirect("Default.aspx",true);
}
else
{
Response.Write("<script>alert('有相同的用户已经登录了系统,如果是非法退出请等待5分钟后再试!');</script>");
}
}
if(result == -1)
{
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, authUser.EncTicket));
Session["UserCode"] = authUser.UserCode;
Session["UserName"] = authUser.UserName;
Session["IsAuthenticated"] = true;
login.UserLogin(authUser.UserCode,"IP:"+Request.UserHostAddress);
Session["LoginID"] = login.LoginID;
Response.Redirect("Default.aspx",true);
}
}
else
{
Response.Write("<script>alert('用户或密码错误!');</script>");
}
}
}
}
}
thbird 2004-10-25
  • 打赏
  • 举报
回复
你可以写个公共模块:
Module basPublic
Public Function CheckPower(ByVal pg As Page, ByVal strPower As String) As Boolean
If pg.Session("Yes") <> "yes" Then
pg.Response.Write("<Font size=7 color=red>对不起,您还没有登录!</Font>")
pg.Response.End()
Else
pg.Session("User").CheckPower(pg, strPower)
End If
End Function

End Module


然后在每页前调动这个模块的CheckPower判断函数。
噯卟釋手 2004-10-25
  • 打赏
  • 举报
回复
简单的说就是利用 Form认证 你可以在登录页面Login.aspx加上这句看看效果

<body onload="if(top != window)top.location.href=window.location.href;">

然后在浏览器里输入别的页面 将自动定向回Login.aspx

噯卟釋手 2004-10-25
  • 打赏
  • 举报
回复
TO: woolgate() 你只看了我的一部分 我并不是通过Session来验证每个页面
而是通过Webconfig里的设置 如果用户不登录Login.aspx 直接输入别的页面的地址
将被自动定向回Login.aspx

<compilation defaultLanguage="c#" debug="true"/>
<customErrors mode="RemoteOnly" defaultRedirect="ErrorPage.htm">
</customErrors>
<authentication mode="Forms">
<forms name="SalesPromotion" loginUrl="Login.aspx" protection="All" path="/">
</forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
<trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true"/>
<sessionState mode="InProc" cookieless="false" timeout="100"/>
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>

关键是这几句

<forms name="SalesPromotion" loginUrl="Login.aspx" protection="All" path="/"> //自动定向页面

<authorization>
<deny users="?"/>
</authorization> //禁止匿名用户访问
woolgate 2004-10-25
  • 打赏
  • 举报
回复
我觉得还是用我顶楼的方法最符合dotNET的思想,那个还能访问的页面好像是缓存,刷新一下页面就回到login.aspx了,用asp时可没这个问题的,不知道怎么解决
thbird 2004-10-25
  • 打赏
  • 举报
回复
可以通过设置全局变量 。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧