asp.net http上下文错误 [英] asp.net http context error
本文介绍了asp.net http上下文错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,程序员在这里有几个问题.
这是我的任务
1.具有表单身份验证的基于角色的授权
所以我创建了一个类
总结..
UserGroupCollection
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
RolesForUser
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
然后在我的web.config
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>
现在,当我设置我的http上下文IVE时,出现了这样的错误消息
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 />
指向
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
推荐答案
MSDN ^ ]GenericPrincipal
构造函数的第一个参数必须是实现IIdentity
接口的对象,而您使用user.UserName
-字符串,绝对不合适.同一篇文章包含一个正确用法的示例,该示例与您的代码略有不同:
MSDN says[^] the first argument forGenericPrincipal
constructor must be an object implementingIIdentity
interface, while you useuser.UserName
- a string, definitely not suitable type. The same article contains an example of correct usage, which just slightly differs from your code:
Dim authenticationType As String = windowsIdentity.AuthenticationType
Dim userName As String = windowsIdentity.Name
Dim genericIdentity = _
New GenericIdentity(userName, authenticationType)
这篇关于asp.net http上下文错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文