关于SESSION的判断!!

zwztu 2003-01-18 04:23:05
请问各位有做项目(如OA)时,因为每个页面都要登入后才能查看,所以每个页面都要判断SESSION值是否存在,我在用ASP时,是把判断SESSION的语句放在一个连接数据库的文件中,因为大部分页面都要调用这个文件,所以我在其它页面不用判断SESSION是否存在了,

但是现在用ASP。NET时,我就不知道要怎么写了(我不想在每个页面都要写上一句判断SESSION的语句),有什么简单的办法,能不能写在web.config文件里(要怎么写),或写在c#类里(因为连接数据库的语句我写成了一个单独的C#类,要用时就using 类名),

或有没有办法其它更好的方法呢?
...全文
305 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jjstar 2003-01-21
  • 打赏
  • 举报
回复
同意luofix(枯古)的看法,下面是我的实现:

这是从前的代码,有点糙,不过思想是对的。
可以把相同访问权的页面都放到同一个目录中,然后对这个目录在web.config进行权限设置。

对web.config如下设置
<configuration>
<authentication mode="Forms">
<forms name="YourApp" path="/" loginUrl="login.aspx" protection="All" timeout="30">
</forms>
</authentication>
<location path="YourFolder">
<system.web>
<authorization>
<allow roles="YourRoles" />
<deny users="*" />
</authorization>
</system.web>
</location>
</configuration>

在Global.asax.vb里面,
If Not (HttpContext.Current.User Is Nothing) Then
If (HttpContext.Current.User.Identity.IsAuthenticated = True) Then
If (HttpContext.Current.User.Identity.AuthenticationType = "Forms") Then
Dim id As FormsIdentity = CType(HttpContext.Current.User.Identity, FormsIdentity)
Dim ticket As FormsAuthenticationTicket = id.Ticket
' Get the stored user-data, in this case, our roles
Dim userData As String = ticket.UserData
Dim roles() As String = userData.Split(",")
HttpContext.Current.User = New GenericPrincipal(id, roles)
End If
End If
End If

在login.aspx.vb中
Dim encryptPwd As String
encryptPwd = Functions.EncryptPassword(txtpwd.Text, "MD5")
Dim reader As SqlClient.SqlDataReader
FormsAuthentication.Initialize()
reader = Functions.GetReaderObject("SELECT name,roles FROM userinfo WHERE Username='" & txtloginid.Text & "' AND Password='" & encryptPwd & "'")
If reader.Read Then
FormsAuthentication.RedirectFromLoginPage(txtloginid.Text, False)
Session("xm") = reader.Item(0)
Dim sqldr As SqlDataReader
sqldr = Functions.GetReaderObject("select distinct YYDX_CZ_BS from SysDefV_XT_QX_YH where YH_BS='" & txtloginid.Text & "'")
Dim inttmp As Integer
Dim objStrB As System.Text.StringBuilder = New System.Text.StringBuilder()
While sqldr.Read
objStrB.Append(sqldr(0).ToString + ",")
End While
If sqldr.IsClosed = False Then
sqldr.Close()
End If
If objStrB.Length > 0 Then
objStrB.Remove(objStrB.Length - 1, 1)
End If
Dim ticket As FormsAuthenticationTicket = New FormsAuthenticationTicket(1, txtloginid.Text, DateTime.Now, DateTime.Now.AddMinutes(30), True, objStrB.ToString, FormsAuthentication.FormsCookiePath)
Dim hash As String = FormsAuthentication.Encrypt(ticket)
Dim cookie As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, hash)
Response.Cookies.Add(cookie)
Else
lblMsg.Text = "用户验证失败!"
lblMsg.Visible = True
End If
If reader.IsClosed = False Then
reader.Close()
End If
milchcow 2003-01-19
  • 打赏
  • 举报
回复
写在一个类里,然后每个页面都调用该类
lbx1979 2003-01-19
  • 打赏
  • 举报
回复
写成一个静态方法也行吧,不麻烦,调用一下就行了
buaawjh 2003-01-19
  • 打赏
  • 举报
回复
我是每个页都写代码了
popcorn 2003-01-19
  • 打赏
  • 举报
回复
我觉得最好的办法还是写在你那个单独的C#类的构造函数里面
使用HttpContext.Current.Session["str"]进行session的判断,这样每个页面都可以访问到了
stdao 2003-01-18
  • 打赏
  • 举报
回复
重建一个类文件,继承于page基类,有这个类文件来判断session
其他的页面这样继承这个类就行了。
详细如下:
pageset.cs
using System;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
using personalization;
using System.Web.SessionState;
public class pageSet : Page
public class pageSet : Page
{
public pageSet()
{

}

private void Page_Load(object sender, System.EventArgs e)
{

if(Object.Equals(Session["UserCode"],null))
{

Response.Redirect("../login.aspx");
}


}

.....


其他的页面如:
a.aspx.cs
namespace oa
{
/// <summary>
/// usermanage 的摘要说明。
/// </summary>
public class a : pageSet
{
。。。。。。




zwztu 2003-01-18
  • 打赏
  • 举报
回复
具体如何实现?请示例。谢谢。
luofix 2003-01-18
  • 打赏
  • 举报
回复
用form验证
ykn 2003-01-18
  • 打赏
  • 举报
回复
好像必须判断阿.我也是这样的.

62,041

社区成员

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

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

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

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