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
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
Public Function RolesForUser(ByVal user As User) As UserGroupCollection
Try
_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
<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