VB.NET – User Impersonation

Application Error:

“ASP.NET  v4.0” does not have the authority to perform the requested command or operation.”

Problem:

ASP.NET  v4.0 application has no right to access DB2 database with default application user.

Solution:

  1. Login on to server where your application is hosted
  2. Create new local user account (myuser, myusrpwd)
  3. Add myuser account to DB2ADMNS, DB2USERS and IIS_IUSRS groups
  4. Open IIS Manager (Start – inetmgr – Enter)
  5. Select Default Web Site
  6. In Default Web Site Home screen, double click Authentication
  7. Right-click ASP.NET  Impersonation and Enable it. Right-click it again and Edit it. Check Specific User: and click Set… button. Enter username and password for the specific user (myuser). Click OK.

<identity impersonate="true" password="myurspwd" userName="myuser" />

  1. Save and x-out Web.config file.
  2. Build and Publish your application.

VB.NET – OleDbConnection Properties

Insert data using OleDbConnection

Use ACE.OLEDB for databse connection (insert data)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles Button1.Click

Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
 Source=C:\Projects\Northwind.accdb;Persist Security Info=False")
Dim CmdStr As String = "insert into sample _
                        (emp_name,category,city) _
                        values ('" & TextBox2.Text & "',_
                        '" & TextBox3.Text & "',_
                         '" & TextBox4.Text & "')"

con.Open()
Dim cmd As OleDbCommand = New OleDbCommand(CmdStr, con)
cmd.ExecuteNonQuery()

con.Close()
MsgBox("Done")

End Sub

Retrieve data using OleDbConnection

Use ACE.OLEDB for databse connection (retrieve data)

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles Button2.Click

Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
 Source=C:\Projects\Northwind.accdb;Persist Security Info=False")
Dim Query2 As String = "select * from Sample"
Dim Da As OleDbDataAdapter, Ds As New DataSet, Dtb As New System.Data.DataTable

con.Open()
Da = New OleDbDataAdapter(Query2, con)
Da.Fill(Ds)

con.Close()

Dtb = Ds.Tables(0)
DataGridView1.DataSource = Dtb

End Sub


How to get GetConnectionString()

VB



Public Shared Function GetConnectionString() As String
      Return ConfigurationManager.ConnectionStrings("ConnectionString").ToString()
       
        ' access properties
        ' if access driver is NOT installed on machine - most possible
        Return "Driver={Microsoft Access Driver (*.mdb, *.accdb)}; 
                 DBQ=C:\Projects\MyProject\DataSource\ProjectDB.accdb"

        ' if access driver is installed on machine
        Return "DSN=CrescoOelweinMerger; 
                DBQ=C:\Projects\MyProject\DataSource\ProjectDB.accdb"
End Function

C#



public static string GetConnectionString()
  {
     return ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
  }