Microsoft Visual Studio – ASP.Net How to Programmatically Unlock a Locked out User

In my previous blog, I showed you how to check to see if a user was locked out and if so, how to manipulate the FailureText of the Login control to notify the user to contact the admin to fix the problem.

In this blog I will show you how to build a simple web form that you as an admin can use to unlock the locked out user.

1) Add a label and textbox to the form that states it for the UserName that is to be unlocked.

2) Add a button to the form that calls the button’s click event.

3) Add the following logic that validates the user entered is a valid user. If not, say so and get out. If so, unlock the user and state that the user is unlocked.

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles btnUnlock.Click
 Dim user As MembershipUser = Membership.GetUser(txtUserName.Text)

If IsNothing(user) = True Then
   Label1.Text = "User not found
   Exit Sub
End If

user.UnlockUser()

If user.IsLockedOut = False Then
   Label1.Text = "User is now unlocked"
Else
   Label1.Text = "Unlock failed. Try again."
End If

End Sub

Microsoft Visual Studio – ASP.Net How to Access the UserName Textbox of a LoginView Control’s Login Control and Manipulate the FailureText

Let’s say you have your web.config set to allow the user only 3 attempts to get the password correct when logging in. After 3 attempts, the profile table sets the isLockedOut column to ‘True’. You want to display this to the user after their 3rd attempt at loggin in.

Behind your Login.aspx form, create the LoginError event and use the following code:

Protected Sub Login1_LoginError(sender As Object, e As System.EventArgs)

   Dim lg As System.Web.UI.WebControls.Login = DirectCast(LoginView1.FindControl("Login1"), System.Web.UI.WebControls.Login)
    Dim tb As TextBox = DirectCast(lg.FindControl("UserName"), TextBox)
    Dim mUser As MembershipUser = Membership.GetUser(tb.Text)

    If mUser.IsLockedOut = True Then
       lg.FailureText = "Too many invalid login attempts. Contact administrator of this website"
    End If

End Sub

The first thing the code does is retrieve the actual UserName textbox from the control as a textbox. The next step takes the string value of the textbox which is the user’s name, calls the GetUser method of the Membership API, and assigns it to your ‘mUser’ MembershipUser object. Next, you test ‘mUser’ to see if the reason for logon failure was due to lock out. If so, modify the FailureText of the Login control to tell the user that they are locked out and to contact their administrator.

 

Windows 7 – Could not find this item – when deleting files

Sometimes applications save files and folders in a way that can’t be deleted via Windows Explorer. The solution is to determine 8.3 format (Short file name) via a command prompt and delete the file within the command prompt dialog as follows:

1) Open a Command Prompt.
2) Browse to the parent folder of the file or folder you want to delete.
3) Type ‘dir /x’ to obtain the 8.3 formatted filename.
4) Delete the file using the 8.3 filename, e.g. ‘del somefilename~1.txt’

SQL Server Reporting Services (SSRS) – How to pass multiple values to a multi value parameter

The short answer is to pass a string array in the value portion of the parameter object.

First, let’s look at the code in a visual basic forms application that builds the parameter and calls the report.

Let’s say we have a collection of last names:

Dim mNameCollection As New List(Of String)
 With mNameCollection
 .Add("DOE")
 .Add("SMITH")
 .Add("MOORE")
 .Add("OBRIEN")
 .Add("LIVINGSTON")
 End With

I am now going to iterate through that collection and build a string array (mStringArray), using the count -1 of my collection to first determine the size of my array.

Dim mStringArray(mNameCollection.Count - 1) As String
Dim mInt As Integer = 0
For Each pObj In mNameCollection
        mStringArray(mInt) = pObj
        mInt += 1
Next

Next, I instantiate a parameter array with one occurrence and pass to it my parameter name of my SSRS report (“LastName” – called @LastName in the SSRS report), and the string array, “mStringArray” as the value for the parameter.

myParms(0) = New Microsoft.Reporting.WinForms.ReportParameter("LastName", mString)

I set the parameters for my report and lastly (not shown here), call the report.

Me.ReportViewer1.ServerReport.SetParameters(myParms)

The SSRS Report

Define an SSRS report. Create a report parameter and name it LastName. Check the box that states ‘Allow multiple values’.  Also, set the parameter visibility to ‘Hidden’.

You can set a default if you want so you can test the report in the solution.

Lastly, in your dataset, use the parameter with the IN clause of your where statement. For example:

SELECT * from name_table
 WHERE last_name IN (@LastName)

Microsoft Visual Studio – ASP.Net How to Mask Your URL by Using a Fake ASPX Name

Let’s say you want to keep hidden from a user of your website the actual aspx page name along with any query string passed to it.

Here is an example of a complete url you want to mask:

www.example.com/original.aspx?parameter1=12345

The first thing you need to do is make up a fake aspx page name and place the following code in your web.config file inside your <system.web> tags like this:

<urlMappings>
 <add url="~/fake.aspx" mappedUrl="~/original.aspx?parameter1=12345"/>
 </urlMappings>

Now, when you create a hyperlink on another aspx page or a user types in the following url into the address bar of an internet browser:

www.example/fake.aspx

it will actually redirect, without the user knowing, to the actual valid url define in your website:

www.example.com/original.aspx?parameter1=12345

 

Microsoft Visual Studio – ASP.Net How to Make a Hyperlink (or Any Other Control) Visible on Page Based on Role

This article assumes you are using roles and have assigned roles to certain users that log in to your website.

Let’s say I have an aspx page with four hyperlinks. I want HyperLink3 and HyperLink4 to be made visible on the page only if the user logged in is in the “managers” role.

Wrap the following “<% %>” logic around your hyperlinks:

<% If User.IsInRole("managers") Then %>
 <p>
   <asp:HyperLink ID="HyperLink3" runat="server" 
 NavigateUrl="~/HL3.aspx" Target="_blank">Some link description</asp:HyperLink>
 </p>
 <p>
 <asp:HyperLink ID="HyperLink4" runat="server" 
 NavigateUrl="~/HL4.aspx" Target="_blank">Some link description</asp:HyperLink>
 </p>
 <% End If %>

SQL Server Management Studio – Studio hangs when trying to select my database hosted by ARVIXE

This post will give you step by step instructions on how to fix the issue in Microsoft SQL Server Management Studio that causes the studio to hang, spin, or loop forever when trying to select a database from the plethora of existing databases stored on your hosted site.

After logging in with the proper server name, user name and password, you will need to follow these instructions:

1) Click “View” and then “Object Explorer Details”. You could also hit the F7 key on your keyboard.
2) Double click on the “Databases” folder from the right panel.
3) Right-click on the column header “Policy Health State”.
4) Deselect the option “Collation”.
5) Click “Refresh”.

Now when selecting your database it won’t hang, even with subsequent instantiations of SQL Management Studio.