none
在communityStarterKit(vb)代码中为什么不能使用session对象?怎样解决? RRS feed

  • 问题

  • 我在communityStarterKit(vb)的“Users_Login.ascx”控件中添加了一个验证码图片,其Url为一个的.asp页面文件,该页面生成验证码图象并将验证码保存在一个session("pcode")里.

    然后我在login.vb代码中加入 session.item("pcode")来作判断,代码编辑窗口提示session没有声明.

    当我声明了"Public session As System.Web.SessionState.HttpSessionState" ,在模块中只能创建新的session对象,而没法读早先生成的session("pcode").

    请教各位怎样解决?

    2009年1月16日 5:22

答案

全部回复

  • System.Web.HttpContext.Current.Session("pcode")
    2009年1月16日 5:38
    版主
  • 十分感谢孟先生,在CSK代码中可以使用session 了。但是用asp生成的验证码(session("pcode")=fcode)保存的session内容为什么读不到呢?页面生成了验证码图片,程序中也加了判断,也想办法显示验证码,但显示为空,输入验证码的文本框为空反倒能通过,为什么?能再帮助指点吗?

    2009年1月17日 5:10
  • 如果没有错误,应该可以读取的。

    另外,需要看一下你的配置,如iis6的话,web园的数目是否为1等。

    2009年1月17日 12:12
    版主
  •  

    是iis6,web园数目也是1 (改成3也不好用)。

    十分麻烦版主帮助看看,问题在哪里。先谢谢孟先生。

     

     

    在users_login.ascx 控件中添加了一个图片控件image2和一个用来输入验证码的文本框txtTest

     

      

    '---------------------------------------------------------------

    '作为image2的“Url”,test1.asp 代码:(拷贝时没有换行)

    '--------------------------------------------------------------

    <%

    Option Explicit Response.buffer=true Call Com_CreatValidCode("GetCode") Sub Com_CreatValidCode(pSN) Response.Expires = -1 Response.AddHeader "Pragma","no-cache" Response.AddHeader "cache-ctrol","no-cache" Response.ContentType = "Image/BMP" Randomize Dim i, ii, iii Const cOdds = 5 ' 杂点出现的机率 Const cAmount = 10 ' 文字数量 Const cCode = "0123456789" ' 颜色的数据(字符,背景) Dim vColorData(1) vColorData(0) = ChrB(0) & ChrB(0) & ChrB(255) ' 蓝0,绿0,红0(黑色) vColorData(1) = ChrB(255) & ChrB(255) & ChrB(255) ' 蓝250,绿236,红211(浅蓝色) ' 随机产生字符 Dim vCode(4), vCodes For i = 0 To 3 vCode(i) = Int(Rnd * cAmount) vCodes = vCodes & Mid(cCode, vCode(i) + 1, 1) Next Session("pcode") = vCodes '------------------------ Dim vNumberData(9) vNumberData(0) = "11100001111101111011110111101111010010111101001011110100" vNumberData(0)=vNumberData(0)&"10111101001011110111101111011110111110000111" vNumberData(1) = "11110111111100011111111101111111110111111111011111111101" vNumberData(1)=vNumberData(1)&"11111111011111111101111111110111111100000111" vNumberData(2) = "11100001111101111011110111101111111110111111110111111110" vNumberData(2)=vNumberData(2)&"11111111011111111011111111011110111100000011" vNumberData(3) = "11100001111101111011110111101111111101111111001111111111" vNumberData(3)=vNumberData(3)&"01111111111011110111101111011110111110000111" vNumberData(4) = "11111011111111101111111100111111101011111101101111110110" vNumberData(4)=vNumberData(4)&"11111100000011111110111111111011111111000011" vNumberData(5) = "11000000111101111111110111111111010001111100111011111111" vNumberData(5)=vNumberData(5)&"10111111111011110111101111011110111110000111" vNumberData(6) = "11110001111110111011110111111111011111111101000111110011" vNumberData(6)=vNumberData(6)&"10111101111011110111101111011110111110000111" vNumberData(7) = "11000000111101110111110111011111111011111111101111111101" vNumberData(7)=vNumberData(7)&"11111111011111111101111111110111111111011111" vNumberData(8) = "11100001111101111011110111101111011110111110000111111011" vNumberData(8)=vNumberData(8)&"01111101111011110111101111011110111110000111" vNumberData(9) = "11100011111101110111110111101111011110111101110011111000" vNumberData(9)=vNumberData(9)&"10111111111011111111101111011101111110001111" ' 输出图像文件头 Response.BinaryWrite ChrB(66) & ChrB(77) & ChrB(230) & ChrB(4) & ChrB(0) Response.BinaryWrite ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) Response.BinaryWrite ChrB(54) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(40) Response.BinaryWrite ChrB(0) & ChrB(0) & ChrB(0) & ChrB(40) & ChrB(0) Response.BinaryWrite ChrB(0) & ChrB(0) & ChrB(10) & ChrB(0) & ChrB(0) Response.BinaryWrite ChrB(0) & ChrB(1) & ChrB(0) ' 输出图像信息头 Response.BinaryWrite ChrB(24) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) Response.BinaryWrite ChrB(0) & ChrB(176) & ChrB(4) & ChrB(0) & ChrB(0) Response.BinaryWrite ChrB(18) & ChrB(11) & ChrB(0) & ChrB(0) & ChrB(18) Response.BinaryWrite ChrB(11) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) Response.BinaryWrite ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) For i = 9 To 0 Step -1 ' 历经所有行 For ii = 0 To 3 ' 历经所有字 For iii = 1 To 10 ' 历经所有像素 ' 逐行、逐字、逐像素地输出图像数据 If Rnd * 99 + 1 < cOdds Then ' 随机生成杂点 Response.BinaryWrite vColorData(0) Else Response.BinaryWrite vColorData(Mid(vNumberData(vCode(ii)), i * 10 + iii, 1)) End If Next Next Next End Sub

    %>

     

     

    '-----------------------------------------------------

    '       在users_login.ascx中点击“登录”后执行的 login.vb代码:

    '-----------------------------------------------------

    Imports System
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports ASPNET.StarterKit.Communities
    Imports System.Web.Security
    Imports System.Web.SessionState.HttpSessionState

     

    '*********************************************************************
    '
    ' Login Class
    '
    ' Represents the user login page which enables
    ' users to login to the community.
    '
    '*********************************************************************

    Public Class Login
        Inherits SkinnedCommunityControl

        Private _skinFileName As String = "Users_Login.ascx"

        Private txtUsername As TextBox
        Private txtPassword As TextBox
        Private chkPersist As CheckBox
        Private btnLogin As Button
        Private pnlInvalidUsername As Panel
        Private pnlInvalidPassword As Panel
        Private lnkPasswordReminder As HyperLink
        Private txtTest As TextBox '后添加的验证码输入框
        Public str1 As String = CType(System.Web.HttpContext.Current.Session("pcode"), String) '后添加的

        '*********************************************************************
        '
        ' Login Constructor
        '
        ' Calls the base SkinnedCommunityControl constructor
        ' and assigns the default page skin.
        '
        '*********************************************************************
        Public Sub New()
            ' Assign a default skin file name
            If SkinFileName Is Nothing Then
                SkinFileName = _skinFileName
            End If
        End Sub 'New

        '*********************************************************************
        '
        ' SkinType Property
        '
        ' Specifies the skins directory where this page's skin file is located.
        '
        '*********************************************************************

        Protected Overrides ReadOnly Property SkinType() As String
            Get
                Return "ContentSkins"
            End Get
        End Property


        '*********************************************************************
        '
        ' InitializeSkin Method
        '
        ' Retrieves all the controls from the Page Skin
        '
        '*********************************************************************
        Protected Overrides Sub InitializeSkin(ByVal skin As Control)
            ' Find the Username TextBox
            txtUsername = CType(GetControl(skin, "txtUsername"), TextBox)

            ' Find the Password TextBox
            txtPassword = CType(GetControl(skin, "txtPassword"), TextBox)
            AddHandler txtPassword.TextChanged, AddressOf btnLogin_Click

            ' Find the Persist Checkbox (Optional)
            chkPersist = CType(GetOptionalControl(skin, "chkPersist"), CheckBox)

            ' Find the Invalid Username Panel
            pnlInvalidUsername = CType(GetControl(skin, "pnlInvalidUsername"), Panel)
            pnlInvalidUsername.Visible = False

            ' Find the Invalid Password Panel
            pnlInvalidPassword = CType(GetControl(skin, "pnlInvalidPassword"), Panel)
            pnlInvalidPassword.Visible = False

            ' Find Login Button
            btnLogin = CType(GetControl(skin, "btnLogin"), Button)
            AddHandler btnLogin.Click, AddressOf btnLogin_Click

            ' Find password reminder
            lnkPasswordReminder = CType(GetControl(skin, "lnkPasswordReminder"), HyperLink)
            lnkPasswordReminder.NavigateUrl = "~/Users_PasswordReminder.aspx"

            ' 后添加的,加载验证码输入框 txtTest TextBox
            txtTest = CType(GetControl(skin, "txtTest"), TextBox)
        End Sub 'InitializeSkin

     

        '*********************************************************************
        '
        ' btnLogin_Click Method
        '
        ' Checks username and password against database. If
        ' everything checks, logins the user.
        '
        '*********************************************************************

        Sub btnLogin_Click(ByVal s As [Object], ByVal e As EventArgs)

            'Dim usme As String = System.Web.HttpContext.Current.Request.Cookies("pcode").Value
            'Dim str1 As String = "shis is :"
            'str1 = str1 & context.Response.Cookies.Item("pcode").Value()
            'context.Response.Write("<script language=javascript> alert(str1); </script>")
            'Exit Sub
            '--------------------
            ' 比较输入的验证码和内存中的session
            '--------------------
            If txtTest.Text <> System.Web.HttpContext.Current.Session("pcode") Then
                context.Response.Write("验证码输入错:")
                context.Response.Write(str1)
                Exit Sub
            End If
            '---------------------

            Dim blPersist As Boolean = False
            ' Determine whether password should be persisted
            If Not (chkPersist Is Nothing) Then
                blPersist = chkPersist.Checked
            End If
            ' Either login or display error message
            Select Case UserUtility.LoginUser(txtUsername.Text, txtPassword.Text)
                Case 0 ' Success!
                    FormsAuthentication.SetAuthCookie(txtUsername.Text, blPersist)
                    Dim redirectUrl As String = FormsAuthentication.GetRedirectUrl(txtUsername.Text, blPersist).ToLower()
                    If redirectUrl.IndexOf("users_logout.aspx") = -1 Then
                        Context.Response.Redirect(redirectUrl)
                    Else
                        Context.Response.Redirect(CommunityGlobals.ResolveBase("default.aspx"))
                    End If
                Case 1 ' Invalid Password
                    pnlInvalidPassword.Visible = True
                    pnlInvalidUsername.Visible = False
                Case 2 ' Invalid Username
                    pnlInvalidUsername.Visible = True
                    pnlInvalidPassword.Visible = False
            End Select
        End Sub 'btnLogin_Click
    End Class 'Login

    2009年1月17日 14:08