Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello programmers got a few question here.
Here is my task
1.Role-Based Authorization With Forms Authentication

so ive created a class
summarized..
UserGroupCollection
VB
Public Function GroupNameToArray() As String()

    Dim temp As New List(Of String)
    For i As Integer = 0 To Me.Count - 1
        temp.Add(Me(i).GroupName)
    Next
    Return temp.ToArray

End Function

RolesForUser
VB
Public Function RolesForUser(ByVal user As User) As UserGroupCollection
      Try
          'select * from User
          _sqlConn = New SqlConnection(_connString)
          _sqlConn.Open()

          Dim sqlCmd As New SqlCommand("RolesForUser", _sqlConn)
          sqlCmd.CommandType = Data.CommandType.StoredProcedure
          sqlCmd.Parameters.AddWithValue("@UserName", user.UserName)

          Dim dr As SqlDataReader = sqlCmd.ExecuteReader
          Dim userGrpColl As New UserGroupCollection
          Dim userGrp As UserGroup = Nothing
          While dr.Read
              userGrp = New UserGroup
              userGrp.GroupID = CInt(dr("GroupID"))
              userGrp.GroupName = dr("GroupName").ToString()

              userGrpColl.Add(userGrp)
          End While

          Return userGrpColl

      Catch ex As Exception
          If _sqlConn IsNot Nothing Then
              If _sqlConn.State = Data.ConnectionState.Open Then
                  _sqlConn.Close()
              End If
          End If
      End Try
      Return Nothing
  End Function

And on my web.config
XML
<authentication mode="Forms">
    <forms loginUrl="Login.aspx"
           protection="All"
           timeout="30"
           name=".ASPXAUTH"
           path="/"
           requireSSL="false"
           slidingExpiration="true"
           defaultUrl="default.aspx"
           cookieless="UseDeviceProfile"
           enableCrossAppRedirects="false" />
  </authentication>

Now as i am setting my http context ive got this eerror saying
Unable to cast object of type 'System.String' to type 'System.Security.Principal.IIdentity'.<br />

Pointing at
HttpContext.Current.User = New GenericPrincipal(user.UserName, _userDAL.RolesForUser(user).GroupNameToArray())

Could you help me debug it?
Thanks and more power
Partial Class Login
    Inherits System.Web.UI.Page
    Private _userDAL As UserDAL
    Private _userrolesDAL As UserRolesDAL
    Private _userGroups As UserGroupDAL
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            _userDAL = New UserDAL
            _userrolesDAL = New UserRolesDAL
            _userGroups = New UserGroupDAL
        End If
    End Sub
    Protected Sub LoginButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim user As New User
        _userDAL = New UserDAL
        user = _userDAL.SelectByUsername(Login1.UserName)
        If user Is Nothing Then
            Login1.FailureText = "Invalid Username"
        Else
            If Login1.Password <> user.Password Then
                Login1.FailureText = "Invalid password"
            Else
                Login1.FailureText = "success"
                _userDAL.RolesForUser(user).GroupNameToArray()
                HttpContext.Current.User = New GenericPrincipal(user.UserName, _userDAL.RolesForUser(user).GroupNameToArray())
            End If
        End If
    End Sub
Posted

1 solution

MSDN says[^] the first argument for GenericPrincipal constructor must be an object implementing IIdentity interface, while you use user.UserName - a string, definitely not suitable type. The same article contains an example of correct usage, which just slightly differs from your code:

VB
Dim authenticationType As String = windowsIdentity.AuthenticationType
Dim userName As String = windowsIdentity.Name
Dim genericIdentity = _
    New GenericIdentity(userName, authenticationType)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900