MembershipUser.ChangePassword Rollback when exception - vb.net

I have implemented the changepassword control and am trying to update password in codebehind. It works! But, when testing negative scenario like giving a password less than the required length, the ChangePassword method throws ArgumentException which is ok but at the same time it updates password in the database.
How can I rollback properly? What are the events fired before throwing the exceptions?
Try
Dim password As String = ChangePwd.NewPassword
'update passwordformat
sp.UpdatePasswordFormat(username, "1")
stepsDone += 1
Dim mu As MembershipUser = Membership.Providers("VLSqlMembershipProvider").GetUser(username, False)
'Dim resetPassword As String = mu.ResetPassword()
' returns false if the current password is invalid)
If (mu.ChangePassword(mu.ResetPassword(), password)) Then
stepsDone += 1
'update account to issecurelyupgraded = 1
sp.UpdateUserSecurelyUpgradedFlag(username)
stepsDone += 1
'Else
'sp.UpdatePasswordFormat(username, "0")
End If
Catch argsex As ArgumentException
sp.UpdatePasswordFormat(username, "0")
Catch ex As Exception
If stepsDone = 1 Then
'roll back updatepasswordformat
sp.UpdatePasswordFormat(username, "0")
ElseIf stepsDone = 2 Then
'roll back first 2 steps
End If
End Try
Some Context
I am migrating from clear to hashed passwordformat and have a custom hash implementation.
<membership defaultProvider="VLSqlMembershipProvider" hashAlgorithmType="customhash">
This is why I am first updating password format from 0 to 1, then updating password and in the end setting my dirty flag to false.

Related

UserPrincipal.Save() - vshost32.exe has stopped working - Corrupt heap

I'm writing some software to import a CSV file into Active Directory (to create user accounts). At some point I know it was working perfectly importing multiple accounts. I'm not sure what I've changed as it's been a while since I last worked on it. But it now imports 2 accounts successfully and then crashes on the line below during the third loop iteration (however the third account is still created):
newUser.Save()
When it crashes I get the error "vshost32.exe has stopped working". I then enabled native code debugging and now get this error: "0xC0000374: A heap has been corrupted" and InvalidCastException (see immediate window at end of post for full error). For testing I've been deleting and recreating the same accounts. If I don't delete the first three accounts, the principal exists exception is handled and then the program crashes on the 4th iteration, and then the 5th and so on. But it never crashes on the first two. (The data I'm importing is identical except for numbers - E.g. sAMAccountNames: Test1, Test2, Test3 etc)
My Code
Private Sub bwImport_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bwImport.DoWork
Dim _worker As BackgroundWorker = CType(sender, BackgroundWorker)
Dim beginImport As New StartImport(AddressOf progressForm.StartImport)
Me.Invoke(beginImport, New Object() {dtUsers.Rows.Count})
Dim log As New UpdateLog(AddressOf progressForm.UpdateLog)
'### TO DO: Check that all mandatory columns/attributes are in the DataTable
'### TO DO: Check for duplicate sAMAccountNames, userPrincipalNames and Cononical Names
#If Not Debug Then
Try
#End If
Dim rowNum As Integer = 0 'Keep track of how many accounts have been created
For Each row As DataRow In dtUsers.Rows
Dim newUser As UserPrincipalEx = New UserPrincipalEx(adCtx)
newUser.SamAccountName = row("sAMAccountName")
newUser.SetPassword(row("Password"))
'General Tab (of Template Exporter)
If row.Table.Columns.Contains("initials") Then
newUser.Initials = row("initials")
End If
If row.Table.Columns.Contains("givenName") Then
newUser.GivenName = row("givenName")
End If
If row.Table.Columns.Contains("sn") Then
newUser.Surname = row("sn")
End If
If row.Table.Columns.Contains("displayName") Then
newUser.DisplayName = row("displayName")
End If
If row.Table.Columns.Contains("description") Then
newUser.Description = row("description")
End If
If row.Table.Columns.Contains("physicalDeliveryOfficeName") Then
newUser.Office = row("physicalDeliveryOfficeName")
End If
If row.Table.Columns.Contains("telephoneNumber") Then
newUser.TelephoneNumber = row("telephoneNumber")
End If
If row.Table.Columns.Contains("wWWHomePage") Then
newUser.WebPage = row("wWWHomePage")
End If
'Address Tab (of Template Exporter)
If row.Table.Columns.Contains("streetAddress") Then
newUser.Street = row("streetAddress")
End If
If row.Table.Columns.Contains("postOfficeBox") Then
newUser.POBox = row("postOfficeBox")
End If
If row.Table.Columns.Contains("l") Then 'City
newUser.City = row("l")
End If
If row.Table.Columns.Contains("st") Then 'State/Province
newUser.State = row("st")
End If
If row.Table.Columns.Contains("postalCode") Then
newUser.PostCode = row("postalCode")
End If
'### TO DO: Add country fields
'Account Tab (of Template Exporter)
If row.Table.Columns.Contains("userPrincipalName") Then
newUser.UserPrincipalName = row("userPrincipalName")
End If
If row.Table.Columns.Contains("ResetPassword") Then
If row("ResetPassword").ToString.ToLower = "yes" Then
newUser.ExpirePasswordNow() 'Force the user to change their password at next logon
End If
End If
If row.Table.Columns.Contains("PreventPasswordChange") Then
If row("PreventPasswordChange").ToString.ToLower = "yes" Then
newUser.UserCannotChangePassword = True
End If
End If
If row.Table.Columns.Contains("PasswordNeverExpires") Then
If row("PasswordNeverExpires").ToString.ToLower = "yes" Then
newUser.PasswordNeverExpires = True
End If
End If
If row.Table.Columns.Contains("AccountDisabled") Then
If row("AccountDisabled").ToString.ToLower = "yes" Then
newUser.Enabled = False
Else
newUser.Enabled = True
End If
Else 'Enable the account by default if not specified
newUser.Enabled = True
End If
If row.Table.Columns.Contains("accountExpires") Then
Dim expireyDate As Date
Date.TryParse(row("accountExpires"), expireyDate) 'Try to convert the data from row("accountExpires") into a date
newUser.AccountExpirationDate = expireyDate
End If
'Profile Tab (of Template Exporter)
If row.Table.Columns.Contains("profilePath") Then
newUser.ProfilePath = row("profilePath")
End If
If row.Table.Columns.Contains("scriptPath") Then
newUser.ScriptPath = row("scriptPath")
End If
If row.Table.Columns.Contains("homeDrive") Then
newUser.HomeDrive = row("homeDrive")
End If
If row.Table.Columns.Contains("homeDirectory") Then
newUser.HomeDirectory = row("homeDirectory")
End If
'Telephones Tab (of Template Exporter)
If row.Table.Columns.Contains("homePhone") Then
newUser.HomePhone = row("homePhone")
End If
If row.Table.Columns.Contains("pager") Then
newUser.Pager = row("pager")
End If
If row.Table.Columns.Contains("mobile") Then
newUser.Mobile = row("mobile")
End If
If row.Table.Columns.Contains("facsimileTelephoneNumber") Then
newUser.Fax = row("facsimileTelephoneNumber")
End If
If row.Table.Columns.Contains("ipPhone") Then
newUser.IPPhone = row("ipPhone")
End If
'Organization Tab
If row.Table.Columns.Contains("title") Then
newUser.Title = row("title")
End If
If row.Table.Columns.Contains("department") Then
newUser.Department = row("department")
End If
If row.Table.Columns.Contains("company") Then
newUser.Company = row("company")
End If
rowNum += 1
_worker.ReportProgress(rowNum) 'Update progress dialog
Try
newUser.Save() 'Save the user to Active Directory
Me.Invoke(log, New Object() {"Successfully created " + row("sAMAccountName") + " (" + row("displayName") + ")", frmProgress.LogType.Success})
Catch ex As PrincipalExistsException
Me.Invoke(log, New Object() {"Error creating " + row("sAMAccountName") + " (" + row("displayName") + "). " + ex.Message, frmProgress.LogType.Failure})
Continue For
End Try
'Member Of Tab
If row.Table.Columns.Contains("MemberOf") Then
Dim groups() As String = row("MemberOf").ToString.Split(";")
'Add the user to any specified groups
Dim groupPrincipal As GroupPrincipal
Try 'Try adding group(s)
For Each group As String In groups
groupPrincipal = groupPrincipal.FindByIdentity(adCtx, group) 'Search for the group name, sid, sAMAccountName or display name
If groupPrincipal IsNot Nothing Then
groupPrincipal.Members.Add(newUser) 'Add the user to the group
groupPrincipal.Save()
Else
Me.Invoke(log, New Object() {"Unable to add " + row("sAMAccountName") + " to group: " + group + ". Group not found.", frmProgress.LogType.Failure})
End If
Next
Catch ex As PrincipalExistsException
'### TO DO: Try to get group name in exception
Me.Invoke(log, New Object() {"Error adding " + row("sAMAccountName") + " (" + row("displayName") + ") to " + "group(s). " + ex.Message, frmProgress.LogType.Failure})
End Try
End If
newUser.Dispose() 'Dispose of the newUser object
Next
#If Not Debug Then
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
#End If
End Sub
Immediate Window (When Native Debugging is enabled)
Critical error detected c0000374
First-chance exception at 0x76fbf996 in AD User Importer.exe: 0xC0000374: A heap has been corrupted.
A first chance exception of type 'System.InvalidCastException' occured in System.DirectoryServices.AccountManagement.dll
I think your issue lies in this block:
Try
newUser.Save() 'Save the user to Active Directory
Me.Invoke(log, New Object() {"Successfully created " + row("sAMAccountName") + " (" + row("displayName") + ")", frmProgress.LogType.Success})
Catch ex As PrincipalExistsException
Me.Invoke(log, New Object() {"Error creating " + row("sAMAccountName") + " (" + row("displayName") + "). " + ex.Message, frmProgress.LogType.Failure})
Continue For
End Try
This code handles exceptions but does not dispose newUser before continuing with the next loop iteration.
I recently began to receive similar heap exceptions and after a close examination I realized that I was not disposing my UserPrincipal object. Once I correctly disposed of the object the issue seems to have stopped.
You should wrap your newUser object in a Using block:
For Each row As DataRow In dtUsers.Rows
Using newUser As UserPrincipalEx = New UserPrincipalEx(adCtx)
newUser.SamAccountName = row("sAMAccountName")
newUser.SetPassword(row("Password"))
' ... the remainder of the code
' ... now wrapped in a Using block
End Using
Next
The newUser object will be automatically disposed no matter how the Using block is exited. Because the Using block disposes for you, you can remove the explicit call to newUser.Dispose().
If the Using block is not available in your version of VB.Net then you should wrap the loop in a Try...Finally block and explicitly dispose the newUser in the Finally block.

How to solve codes after for loop that do not execute in vb.net?

I have this code under my form_load
checkUser = False
MsgBox("test start")
result = Newtonsoft.Json.JsonConvert.DeserializeObject(Of ArrayList)(getJSon("https://dtitdtr.herokuapp.com/employees"))
MsgBox("test after result before for-each")
For Each value As Object In result
token = JObject.Parse(value.ToString())
id = token.SelectToken("id")
fname = token.SelectToken("fname")
mname = token.SelectToken("mname")
lname = token.SelectToken("lname")
contact = token.SelectToken("contactno")
add = token.SelectToken("address")
user = token.SelectToken("username")
pass = token.SeelectToken("password")
If user.ToString().ToUpper().Equals(GetUName()) Then
checkUser = True
Exit For
Else
checkUser = False
End If
Next value
MsgBox("test after next value")
reader.Close()
response.Close()
and when I run the program, the first two(2) message boxes displayed and the last one, which is after the Next Value, won't display.
I don't quite get what's going on; since yerterday, when I run it, it went just fine and right now after adding codes for update info, which does not suppose to affect the form_load, the codes right after for loop won't execute. What is the problem with this?
I got something here that says "an exception is being thrown" but I don't even have a Try Catch in my code.
Hope that your code throws some Exception inside the For, so i suggest you to include Try.. Catch get the exception details. that helps you to detect the problem.
Try
For Each value As Object In result
token = JObject.Parse(value.ToString())
id = token.SelectToken("id")
fname = token.SelectToken("fname")
mname = token.SelectToken("mname")
lname = token.SelectToken("lname")
contact = token.SelectToken("contactno")
add = token.SelectToken("address")
user = token.SelectToken("username")
pass = token.SeelectToken("password")
If user.ToString().ToUpper().Equals(GetUName()) Then
checkUser = True
Exit For
Else
checkUser = False
End If
Next value
MsgBox("test after next value")
reader.Close()
response.Close()
Catch ex As Exception
MsgBox("Exception :" & ex.ToString)
End Try

Setting ComboBox Selected Value to AD Query

I'm running an AD query to pull selected attributes from a users profile. I'm selecting extensionAttribute3, 4, 5, 6, 7 & 8. Although I can get the result to display as text, I'd like to set the selected vlaue of a combobox to the results.
So extension attribute 3, 5 & 7 = security questions, 4, 6 & 8 are the answers. I have 3 comboboxes, each with a list of 15 possible security questions users can select from, and then provide answers to. I've got my script to update AD with the questions & answers selected. However when I run the application again, I'd like to pull the existing questions from extensionAttribute 3, 5 & 7, as set as the default selected foreach combobox.
Current AD Query Code:
Private Function GetUserProperties()
Dim ADName As String = GetLogonName()
Dim CurrentPIN As String = Nothing
Dim bSuccess As Boolean = False
Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
Dim Q1Value As String = Nothing
dirSearcher.Filter = ("(samAccountName=" & ADName & ")")
dirSearcher.PropertiesToLoad.Add("extensionAttribute3")
dirSearcher.PropertiesToLoad.Add("extensionAttribute4")
dirSearcher.PropertiesToLoad.Add("extensionAttribute5")
dirSearcher.PropertiesToLoad.Add("extensionAttribute6")
dirSearcher.PropertiesToLoad.Add("extensionAttribute7")
dirSearcher.PropertiesToLoad.Add("extensionAttribute8")
dirSearcher.SearchScope = SearchScope.Subtree
Try
Dim dirResult As SearchResult = dirSearcher.FindOne()
bSuccess = Not (dirResult Is Nothing)
If dirResult Is Nothing OrElse dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value Is Nothing Then
Return "<not set>"
Else
Q1Value = dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value.ToString
Q1ComboBox.SelectedIndex = Q1Value
End If
Catch ex As Exception
bSuccess = False
MsgBox("No Connection to the domain." & Environment.NewLine & "Please connect to corporate network & try again.", MsgBoxStyle.Critical, "Network Error")
Application.Exit()
End Try
Return False
End Function
It's really hard to format code in comments, i put them here instead.
I'm not VB programmer, may have syntax error.
You don't provide code for extensionAttribute4-8, so it's hard to find what's wrong with them. Do you mean for extensionAttribute4-8, just repeating the if-else block inside the try-catch does not work?
For example, you cannot get value of extensionAttribute4 below?
' code for extensionAttribute3, omitted here
....
' code for extensionAttribute4
If dirResult Is Nothing OrElse dirResult.GetDirectoryEntry.Properties("extensionAttribute4").Value Is Nothing Then
Return "<not set>"
Else
A1Value = dirResult.GetDirectoryEntry.Properties("extensionAttribute4").Value.ToString
A1ComboBox.SelectedIndex = A1Value
End If
' repeat for extensionAttribute5-8
....
For using the attribute already loaded in SearchResult, you already handle the conversion to string problem (mentioned in comment) by calling ToString. You can just do the same thing. But instead of checking dirResult.GetDirectoryEntry.Properties("...").Value Is Nothing, you should check dirResult.Properties("...").Count > 0.
Dim dirResult As SearchResult = dirSearcher.FindOne()
bSuccess = Not (dirResult Is Nothing)
If dirResult Is Nothing OrElse dirResult.Properties("extensionAttribute3").Count <= 0 Then
Return "<not set>"
Else
Q1Value = dirResult.Properties("extensionAttribute3")[0].ToString
Q1ComboBox.SelectedIndex = Q1Value
End If

Failed to Enable Constraints in SQL

I'm currently working on a bit of homework for school, and am almost done, however I've got an issue with trying to run a query to match some user data. Every time I run the debugging process, it comes up time and again with the issue of 'Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.' I'm not sure what I'm doing wrong, or how to get past it. Is there something up with my query, or with my code?
Public Class Login_Processing
Dim adapter As New RRBCDataSetTableAdapters.LoginTableAdapter
Dim blnPass As Boolean
Public Function Login(ByVal Username As String, ByVal Password As String) As Boolean
Try
If adapter.GetUserNames(Username).ToString = "VViscioni" Or _
adapter.GetUserNames(Username).ToString = "Whiter" Then
If adapter.GetPassword(Username).ToString = Password Then
frmMain.tsiAdmin.Enabled = True
frmMain.tsiEAdmin.Enabled = True
frmMain.tsiEPlayer.Enabled = True
MessageBox.Show("Welcome back!")
blnPass = True
Else
MsgBox("Is this a new user?", MsgBoxStyle.YesNo)
If vbYes Then
AddAUser.ShowDialog()
Else
MessageBox.Show("Please re-input your password.")
blnPass = False
End If
End If
ElseIf adapter.GetUserNames(Username).ToString = Username Then
If adapter.GetPassword(Username).ToString = Password Then
frmMain.tsiEPlayer.Enabled = True
MessageBox.Show("Welcome to the Roadrunners Baseball Club!")
blnPass = True
Else
MessageBox.Show("Please re-input your password.")
blnPass = False
End If
End If
Catch ex As Exception
MessageBox.Show("Please re-input your username/password.")
blnPass = False
End Try
Return blnPass
End Function
End Class
Here's the SQL queries I'm trying to use for the adapters:
GetUserName Query:
SELECT Login FROM Login
WHERE (Login = #Login)
GetPassword Query:
SELECT Password FROM Login
WHERE (Login = #Login)
The parameters in the function relate to a Username and Password entry the user has to input.

To iterate through the values of combo box control using vb.net

I update my question here .. Am using a combo box with no of phone numbers .I want to get the phone no one by one in a variable. Now am using the below code to get the combobox values. But still now am getting the following error message System.Data.DataRowView. Please help me to fix this error. am new for vb.net.
My partial code is here ..
For i = 0 To ComboBox1.Items.Count
Dim s As String
s = Convert.ToString(ComboBox1.Items(i))
Next i
you are using an index which is zero based.
change this:
For i = 0 To ComboBox1.Items.Count
to this:
For i = 0 To ComboBox1.Items.Count - 1
This also works!
Dim stgTest = "Some Text"
Dim blnItemMatched As Boolean = False
'-- Loop through combobox list to see if the text matches
Dim i As Integer = 0
For i = 0 To Me.Items.Count - 1
If Me.GetItemText(Me.Items(i)) = stgTest Then
blnItemMatched = True
Exit For
End If
Next i
If blnItemMatched = False Then
Dim stgPrompt As String = "You entered '" & stgTypedValue & "', which is not in the list."
MessageBox.Show(stgPrompt, "Incorrect Entry", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.Text = ""
Me.Focus()
End If
Your problem probably happens here:
s = Convert.ToString(ComboBox1.Items(i))
This doesn't return the value. It returns a string representation of the object at the given index, which in your case apparently is of type System.Data.DataRowView.
You would have to cast ComboBox1.Items(i) to the approbriate type and access its Value. Or, since its a DataRowView, you can access the values throgh the appropriate column names:
Dim row = CType(ComboBox1.Items(i), System.Data.DataRowView)
s = row.Item("column_name")
Nevertheless, first of all you should definitely close and dispose the connection, no matter whether the transaction fails or succeeds. This can be done in a finally block (option 1) or with a using statement (option 2).
Option 1
// ...
con1 = New MySqlConnection(str)
con1.Open()
Try
// ...
Catch ex As Exception
Lblmsg.Text = " Error in data insertion process....." + ex.Message
Finally
con1.Close()
con1.Dispose()
End Try
Option 2
// ...
Using con1 as New MySqlConnection(str)
con1.Open()
Try
// ...
Catch ex As Exception
Lblmsg.Text = " Error in data insertion process....." + ex.Message
Finally
con1.Close()
End Try
End using
Even after long time back you will achieve this with simply by following
For Each item As Object In combx.Items
readercollection.Add(item.ToString)
Next
Please try this
For j As Integer = 0 To CboCompany.Items.Count - 1
Dim obj As DataRowView = CboCompany.Items(j)
Dim xx = obj.Row(0)
If xx = "COMP01" Then
CboCompany.SelectedIndex = j
Exit For
End If
Next
I could not find this answer online in its entirety but pieced it together. In the snippet below cbox is a ComboBox control that has the DisplayMember and ValueMember properties initialized.
Dim itemIE As IEnumerator = cbox.Items.GetEnumerator
itemIE.Reset()
Dim thisItem As DataRowView
While itemIE.MoveNext()
thisItem = CType(itemIE.Current(), DataRowView)
Dim valueMember As Object = thisItem.Row.ItemArray(0)
Dim displayMember As Object = thisItem.Row.ItemArray(1)
' Insert code to process this element of the collection.
End While