Im trying to convert a SQL login script with roles into a Linq Script.
Im getting the login Values fine too the query, but im not getting to the default page.
Can someone help/guide me !?
Private Function ValidateUser(ByVal userName As String, ByVal passWord As String) As Boolean
Dim lookupPassword As String
lookupPassword = Nothing
' Check for an invalid userName.
' userName must not be set to nothing and must be between one and 15 characters.
If ((userName Is Nothing)) Then
System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName failed.")
Return False
End If
If ((userName.Length = 0) Or (userName.Length > 15)) Then
System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName failed.")
Return False
End If
' Check for invalid passWord.
' passWord must not be set to nothing and must be between one and 25 characters.
If (passWord Is Nothing) Then
System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed.")
Return False
End If
If ((passWord.Length = 0) Or (passWord.Length > 25)) Then
System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed.")
Return False
End If
Using db As New DCbinderDataContext()
Try
Dim getUser As IQueryable(Of User) = From c In db.Users Where c.Username = userName And c.Password = passWord Select c
' Execute command and fetch pwd field into lookupPassword string.
lookupPassword = getUser.ToString
Catch ex As Exception
' Add error handling here for debugging.
' This error message should not be sent back to the caller.
System.Diagnostics.Trace.WriteLine("[ValidateUser] Exception " & ex.Message)
End Try
End Using
' If no password found, return false.
If (lookupPassword Is Nothing) Then
' You could write failed login attempts here to the event log for additional security.
Return False
End If
' Compare lookupPassword and input passWord by using a case-sensitive comparison.
Return (String.Compare(lookupPassword, passWord, False) = 0)
End Function
Private Sub cmdLogin_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdLogin.ServerClick
If ValidateUser(txtUserName.Value, txtUserPass.Value) Then
Dim tkt As FormsAuthenticationTicket
Dim cookiestr As String
Dim ck As HttpCookie
tkt = New FormsAuthenticationTicket(1, txtUserName.Value, DateTime.Now(), DateTime.Now.AddMinutes(30), chkPersistCookie.Checked, "your custom data")
cookiestr = FormsAuthentication.Encrypt(tkt)
ck = New HttpCookie(FormsAuthentication.FormsCookieName(), cookiestr)
If (chkPersistCookie.Checked) Then ck.Expires = tkt.Expiration
ck.Path = FormsAuthentication.FormsCookiePath()
Response.Cookies.Add(ck)
Dim strRedirect As String
strRedirect = Request("ReturnURL")
If strRedirect <> "" Then
Response.Redirect(strRedirect, True)
Else
strRedirect = "Default.aspx"
Response.Redirect(strRedirect, True)
End If
Else
Response.Redirect("DbLogin.aspx", True)
End If
End Sub
And how do i convert this SQL/Select string into linq !?
Dim cmd As New OleDbCommand("Select Groups.Name FROM ((Roles INNER JOIN Groups ON Groups.GroupID = Roles.GroupID) INNER JOIN Users ON USERS.UserID = Roles.UserID) WHERE Users.Username=@UserName", conn)
cmd.Parameters.AddWithValue("@UserName", User.Identity.Name)
Dim reader As OleDbDataReader = cmd.ExecuteReader()
Dim roleList As New ArrayList()
While reader.Read()
roleList.Add(reader("Name"))
End While
Dim roleListArray As String() = DirectCast(roleList.ToArray(GetType(String)), String())
HttpContext.Current.User = New GenericPrincipal(User.Identity, roleListArray)
reader.Close()
conn.Close()
Dim getUser As IQueryable(Of User) = From c In db.Users Where c.Username = userName And c.Pa
If you can decide that there's only one User when you confirm your UserName and Password, you can use FirstOrDefault to deal with that——
Dim getUser As User = (From c In db.Users Where c.Username = userName And c.Password).FirstOrDefault()
If (getUser IsNot Nothing) Then
………………
End If
siraero
Dim cmd As New OleDbCommand("Select Groups.Name FROM ((Roles INNER JOIN Groups ON Groups.GroupID = Roles
You can try this:
var result = from role in Roles
join g in Groups on role.Id equal g.Id into temp
select new
{
Groups = from t in temp
select new
{
GroupName = t.Name
}
}
//Then do this:
foreach(var item result)
{
foreach(var sitem in item.Groups)
{
//do what u wanna……
}
}
siraero
Member
419 Points
604 Posts
Converting a SQL login script too Linq Login Script.
Sep 24, 2012 08:25 PM|LINK
Hi
Im trying to convert a SQL login script with roles into a Linq Script.
Im getting the login Values fine too the query, but im not getting to the default page.
Can someone help/guide me !?
Private Function ValidateUser(ByVal userName As String, ByVal passWord As String) As Boolean Dim lookupPassword As String lookupPassword = Nothing ' Check for an invalid userName. ' userName must not be set to nothing and must be between one and 15 characters. If ((userName Is Nothing)) Then System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName failed.") Return False End If If ((userName.Length = 0) Or (userName.Length > 15)) Then System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName failed.") Return False End If ' Check for invalid passWord. ' passWord must not be set to nothing and must be between one and 25 characters. If (passWord Is Nothing) Then System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed.") Return False End If If ((passWord.Length = 0) Or (passWord.Length > 25)) Then System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed.") Return False End If Using db As New DCbinderDataContext() Try Dim getUser As IQueryable(Of User) = From c In db.Users Where c.Username = userName And c.Password = passWord Select c ' Execute command and fetch pwd field into lookupPassword string. lookupPassword = getUser.ToString Catch ex As Exception ' Add error handling here for debugging. ' This error message should not be sent back to the caller. System.Diagnostics.Trace.WriteLine("[ValidateUser] Exception " & ex.Message) End Try End Using ' If no password found, return false. If (lookupPassword Is Nothing) Then ' You could write failed login attempts here to the event log for additional security. Return False End If ' Compare lookupPassword and input passWord by using a case-sensitive comparison. Return (String.Compare(lookupPassword, passWord, False) = 0) End Function Private Sub cmdLogin_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdLogin.ServerClick If ValidateUser(txtUserName.Value, txtUserPass.Value) Then Dim tkt As FormsAuthenticationTicket Dim cookiestr As String Dim ck As HttpCookie tkt = New FormsAuthenticationTicket(1, txtUserName.Value, DateTime.Now(), DateTime.Now.AddMinutes(30), chkPersistCookie.Checked, "your custom data") cookiestr = FormsAuthentication.Encrypt(tkt) ck = New HttpCookie(FormsAuthentication.FormsCookieName(), cookiestr) If (chkPersistCookie.Checked) Then ck.Expires = tkt.Expiration ck.Path = FormsAuthentication.FormsCookiePath() Response.Cookies.Add(ck) Dim strRedirect As String strRedirect = Request("ReturnURL") If strRedirect <> "" Then Response.Redirect(strRedirect, True) Else strRedirect = "Default.aspx" Response.Redirect(strRedirect, True) End If Else Response.Redirect("DbLogin.aspx", True) End If End SubAnd how do i convert this SQL/Select string into linq !?
Dim cmd As New OleDbCommand("Select Groups.Name FROM ((Roles INNER JOIN Groups ON Groups.GroupID = Roles.GroupID) INNER JOIN Users ON USERS.UserID = Roles.UserID) WHERE Users.Username=@UserName", conn) cmd.Parameters.AddWithValue("@UserName", User.Identity.Name) Dim reader As OleDbDataReader = cmd.ExecuteReader() Dim roleList As New ArrayList() While reader.Read() roleList.Add(reader("Name")) End While Dim roleListArray As String() = DirectCast(roleList.ToArray(GetType(String)), String()) HttpContext.Current.User = New GenericPrincipal(User.Identity, roleListArray) reader.Close() conn.Close()Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Converting a SQL login script too Linq Login Script.
Sep 26, 2012 02:01 AM|LINK
If you can decide that there's only one User when you confirm your UserName and Password, you can use FirstOrDefault to deal with that——
You can try this:
var result = from role in Roles join g in Groups on role.Id equal g.Id into temp select new { Groups = from t in temp select new { GroupName = t.Name } } //Then do this: foreach(var item result) { foreach(var sitem in item.Groups) { //do what u wanna…… } }