无法根据角色

时间:2018-03-22 21:02:14

标签: vba redirect login

我在登录时根据他们的角色重定向用户有些困难。当我登录用户/管理员时,他们会被重定向到Default.aspx而不是指定的页面。我一直在看这个代码太多,看不出有什么问题。关于我遇到这个问题的原因,我将非常感激。

这是我的Login.aspx

    If you have don't have an account, <a href="CreateUser.aspx">Create one here!</a><br />
    <br />
&nbsp;<asp:Login ID="Login1" runat="server" DisplayRememberMe="true">
    </asp:Login>

这是我的Login.aspx.vb

Imports System.Data.SqlClient
Imports System.Data
Imports System.Configuration
Imports System.Web.Security

Partial Class ContentPages_Login
    Inherits System.Web.UI.Page

    Protected Sub page_load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            If Me.Page.User.Identity.IsAuthenticated Then
                FormsAuthentication.SignOut()
                Response.Redirect("~/ContentPages/Login.aspx")
            Else
                Session.Abandon()
                Session.Clear()
            End If
        End If

    End Sub
    Protected Sub Login1_Authenticate(sender As Object, e As AuthenticateEventArgs) Handles Login1.Authenticate
        FormsAuthentication.Initialize()

        Dim USER_ID As Integer = 0
        Dim roles As String = String.Empty
        Dim con As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True")

        Using cmd As New SqlCommand("Validate_User")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@USER_NAME", Login1.UserName)
            cmd.Parameters.AddWithValue("@PASSWORD", Login1.Password)
            cmd.Connection = con
            con.Open()
            Dim reader As SqlDataReader = cmd.ExecuteReader()
            reader.Read()
            USER_ID = Convert.ToInt32(reader("USER_ID"))
            roles = reader("Roles").ToString()
            con.Close()
        End Using
        Select Case USER_ID
            Case -1
                Login1.FailureText = "Username and/or password is incorrect."
            Case -2
                Login1.FailureText = "Account has not been activated."
                Exit Select
            Case Else
                Dim ticket As New FormsAuthenticationTicket(1, Login1.UserName, DateTime.Now, DateTime.Now.AddMinutes(2880), Login1.RememberMeSet, roles, FormsAuthentication.FormsCookiePath)
                Dim hash As String = FormsAuthentication.Encrypt(ticket)
                Dim cookie As New HttpCookie(FormsAuthentication.FormsCookieName, hash)

                If ticket.IsPersistent Then
                    cookie.Expires = ticket.Expiration
                End If
                Response.Cookies.Add(cookie)
                Response.Redirect(FormsAuthentication.GetRedirectUrl(Login1.UserName, Login1.RememberMeSet))

                If Request.IsAuthenticated AndAlso User.IsInRole("Admin") = True Then
                    Response.Redirect("~/AdminPages/CustomerList.aspx")
                ElseIf Request.IsAuthenticated AndAlso User.IsInRole("User") = True Then
                    Response.Redirect("~/ClientPages/CustomerProfile.aspx")
                End If

                Exit Select
        End Select

    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

I didn't have the correct location path ="~/foldername" in the webconfig. Now works as intended

相关问题