0

次のコードを含むマスターページがあります。

<script runat="server">



    Protected Sub Page_Load(sender As Object, e As EventArgs)

        If Session("userid") = Nothing Then
            txtLoginUser.Visible = True
            txtLoginPass.Visible = True

        Else


            Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
            Dim useridComm As String = "SELECT name, surname FROM users WHERE user_id=@userid"
            Dim sqlUserID As New SqlCommand

            conn.Open()

            Dim userid As String = Session("UserID")

            sqlUserID = New SqlCommand(useridComm, conn)
            sqlUserID.Parameters.AddWithValue("@userid", Convert.ToInt32(userid))
            Dim datareader As SqlDataReader = sqlUserID.ExecuteReader()

            datareader.Read()
            If datareader.HasRows Then



                userid = Session("UserID")

                lblLoggedIn.Text = "[Welcome, " + datareader("name").ToString() & " " & datareader("surname").ToString() + " ]"
                txtLoginUser.Visible = False
                txtLoginPass.Visible = False
                lblUsername.Visible = False
                lblRegister.Visible = False
                btnLogin.Visible = False
                lblUsername0.Visible = False


            End If
            datareader.Close()
            conn.Close()

        End If
    End Sub

   Protected Sub Button1_Click(sender As Object, e As EventArgs)

        Dim loginSQL As New SqlCommand
        Dim loginComm As String

        Dim CommonFunctions As New CommonFunctions()
        Dim dec_pass As String = CommonFunctions.EncryptPassword(txtLoginPass.Text.Trim)

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")


        loginComm = "SELECT user_id FROM users WHERE username=@username and password=@password"

        conn.Open()


        loginSQL = New SqlCommand(loginComm, conn)
        loginSQL.Parameters.AddWithValue("@username", txtLoginUser.Text.ToString)
        loginSQL.Parameters.AddWithValue("@password", dec_pass)
        Dim dr As SqlDataReader = loginSQL.ExecuteReader()
        dr.Read()


        If dr.HasRows Then
            Session("UserID") = dr("user_id")

        ElseIf dr.HasRows = False Then

            lblRegister.ForeColor = Drawing.Color.Red
            lblRegister.Text = "Incorrect Username/Password."
        End If


        dr.Close()
        conn.Close()

        Response.Redirect("Default.aspx")

    End Sub
</script>

Button1 をクリックすると、スクリプトは を使用して user_id を取得し、datareaderを作成してSession("UserID")に渡しDefault.aspxます。Default.aspx次に を取得し、同じ値を持つSession("UserID")を検索し、テーブルuser_idを使用してロールをチェックします。が 4 の場合は表示され、それ以外の場合は表示されません。user_rolesrole_idtblAdmin

これは Default.aspx のコードです。

Imports System.Data.SqlClient

Partial Class _Default
    Inherits System.Web.UI.Page



    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load


        Dim UserID As Integer = Convert.ToInt32(Session("UserID"))

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim userTypeCommand As String = "SELECT role_id FROM users_role WHERE user_id=@UserID"
        Dim userTypeSQL As New SqlCommand

        conn.Open()

        Try

            userTypeSQL = New SqlCommand(userTypeCommand, conn)
            userTypeSQL.Parameters.AddWithValue("@UserID", UserID)

            Dim datareader As SqlDataReader = userTypeSQL.ExecuteReader

            If datareader("role_id").ToString = "4" Then

                tblAdmin.Enabled = True
                tblAdmin.Visible = True

            ElseIf datareader("role_id").ToString IsNot "4" Then

                tblAdmin.Visible = False

            End If



        Catch ex As Exception



        End Try


        conn.Close()


    End Sub

    Protected Sub btnCreateArticle_Click(sender As Object, e As EventArgs) Handles btnCreateArticle.Click


        Response.Redirect("addArticle.aspx")

    End Sub

    Protected Sub btnAdmin_Click(sender As Object, e As EventArgs) Handles btnAdmin.Click
        Response.Redirect("Admin.aspx")
    End Sub
End Class

デバッグ時に「ログイン」ボタンを押した後、ログインに使用したユーザーの user_id が表の「12」の場合、user_id (Session('UserID') は 0 のままです。

私は何を間違っていますか?

ASP.NET/VB.NET と SQL Server 2012 を使用しています。

4

1 に答える 1