Visual Studio asp.net Web应用程序中不同网站上不同用户的菜单不同,而不是网站 [英] different menu for different users in visual studio asp.net web application not website

查看:54
本文介绍了Visual Studio asp.net Web应用程序中不同网站上不同用户的菜单不同,而不是网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



首先,我有3个用户,一个管理员和一名学生,他们在同一页面登录,这是登录页面的代码










hey

first i have 3 users an admin a tutor and a student who logon in the same page this is the code of the login page










Imports System.Data.SqlClient
Partial Public Class login
    Inherits System.Web.UI.Page

    Protected Sub loginbutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles loginbutton.Click

        If AuthenticateUser(loginnamebox.Text, passwordbox.Text) Then
            CreateAuthenticationTicket(loginnamebox.Text, remembermecheckbox.Checked)

        Else

            label1.Text = "Wrong username or password, please try again"


        End If



    End Sub



    Private Function AuthenticateUser(ByVal Username As String, _
ByVal Password As String) As Boolean


        Dim sqlCon As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        Dim query As String = "Select username from users where username = '" & Username & "'  and password  = '" & Password & "'"

        Dim sqlCmd As New SqlCommand(query, sqlCon)

        Dim reader As SqlDataReader



        sqlCon.Open()

        reader = sqlCmd.ExecuteReader()


        Return reader.HasRows
        sqlCon.Close()





    End Function

    Private Function GetUserRoles(ByVal username As String) As String

        Dim sqlCon As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        Dim query As String = "Select role from users where username =  '" & username & "'"

        Dim sqlCmd As New SqlCommand(query, sqlCon)
        Dim reader As SqlDataReader

        sqlCon.Open()

        reader = sqlCmd.ExecuteReader(Data.CommandBehavior.CloseConnection)


        reader.Read()

        Dim role As String = reader.Item(0).trim()


        sqlCon.Close()

        Return role






    End Function

    Private Sub CreateAuthenticationTicket(ByVal userName As String, ByVal isPersistent As Boolean)


        Dim version As String = 1
        Dim issueDate As DateTime = Now
        Dim expirationDate As Date
        Dim userData As String = GetUserRoles(userName)

        Dim cookiePath As String = "/"


        'Set the expirationDate
        If isPersistent Then
            expirationDate = Now.AddYears(1)
        Else
            expirationDate = Now.AddMinutes(60)
        End If


        'Set up the authentication ticket
        Dim FormAuthTicket As FormsAuthenticationTicket = _
        New FormsAuthenticationTicket(version, userName, issueDate, _
        expirationDate, isPersistent, userData, cookiePath)



        'Encrypt the ticket content as a string so it can be stored in a cookie
        Dim encTicket As String = FormsAuthentication.Encrypt(FormAuthTicket)

        'Place the encrypted ticket in a cookie
        Dim AuthCookie As HttpCookie = _
        New HttpCookie(FormsAuthentication.FormsCookieName, encTicket)


        'Set cookie duration if necessary
        If isPersistent Then AuthCookie.Expires = Now.AddYears(1)

        'Send cookie back to user
        Response.Cookies.Add(AuthCookie)

        'Redirect user to the page from whence they came
        Response.Redirect(FormsAuthentication.GetRedirectUrl(userName, isPersistent))


    End Sub






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

    End Sub
End Class


................................................... .....................


设置为母版页,并且该页内的代码为





.......................................................................


as master page is set and the code inside the page is




Public Partial Class main
    Inherits System.Web.UI.MasterPage
    Private menu As String



    Public ReadOnly Property getmenu() As String
        Get
            Return menu

        End Get
    End Property


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



        If Context.User.IsInRole("Student") Then


            menu = "<tr valign=""top""> <td> <a href=""ViewMemo.aspx"" > View Memo</a> </td> </tr> <tr valign=""top""> <td> <a href=""StudentChapter.aspx"" > View Chapters</a> </td> </tr> <tr valign=""top""> <td> <a href=""StudentExam.aspx""> Take Exam</a> </td>  </tr> <tr valign=""top""> <td><a href=""ViewExamGrades.aspx""> View Exam Grades</a></td></tr><tr valign=""top""><td><a href=""ChangePassword.aspx""> Change Password </a></td></tr>"

        ElseIf Context.User.IsInRole("Tutor") Then

            menu = " <tr valign=""top""> <td> <a href=""ChapterCreate.aspx"" >  Create Chapter  </a></td></tr><tr valign=""top""><td><a href=""EditChapter.aspx"" > Edit Chapter</a></td></tr><tr valign=""top""><td><a href=""CreateExamPage.aspx"" > Create Exam</a></td></tr><tr valign=""top""><td><a href=""ManageExams.aspx""> Manage Exams</a></td></tr> <tr valign=""top""><td><a href=""CreateMemo.aspx"" > Create Memo</a></td></tr><tr valign=""top""><td><a href=""ViewExamGrades.aspx""> View Exam Grades</a></td></tr> <tr><td><a href=""ViewStudentLevel.aspx""> View Student Levels </a> </td> </tr> <tr> <td> <a href=""ChangePassword.aspx""> Change Password </a></td></tr>  "

        ElseIf Context.User.IsInRole("Admin") Then

            menu = " <tr valign=""top""><td><a href=""CreateUser.aspx""> Create User</a></td></tr><tr valign=""top""> <td> <a href=""ManageUsers.aspx"" > Manage Users</a></td></tr> "
        End If

    End Sub



    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton1.Click

    End Sub
End Class



................................................... ......................

在内容的表的参数之间设置了get菜单语句以显示菜单

设置了fefault页并具有母版页表单


我之前在网站上尝试过此功能,为什么id现在在Web应用程序中不起作用?

我真的需要帮助

REGARDS



........................................................................

a get menu statement is set between the paremeters of a table in the content to display the menu

a fefault page is set and have a master page form


i tried this before in a website it worked, why id doesnt work now in a web application?

i really need help

REGARDS

推荐答案

这绝对可怕!不要以这种方式为您编写html内容,并且一定不要在页面加载事件中全部这样做.
阅读有关ASP.NET开发的一些书籍/文章,然后在了解标记和代码隐藏之间的区别之后再问问题.
This is absolutely horrible! DO NOT write you html content this way and certainly do no do it all in the page load event.
Go read some books/articles on ASP.NET development, then come back and ask questions after you understand the difference between markup and code-behind.


这篇关于Visual Studio asp.net Web应用程序中不同网站上不同用户的菜单不同,而不是网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆