asked on
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
Dim i As Integer = 0
'Dim myConn As New SqlConnection(myConnStr)
Dim myRow As DataRow
Dim mUser As MembershipUser = (Session("myUser"))
On Error GoTo Problem
For i = 0 To myDS.Tables(1).Rows.Count - 1
myRow = myDS.Tables(1).Rows(i)
Dim myCMD As New SqlCommand
With myCMD
.Connection = myConn
.CommandType = CommandType.StoredProcedure
.CommandText = "TAG_UpdateMOInspectionDetail_Web"
.Parameters.Add(New SqlParameter("@ItemQty", myRow.Item(5).ToString))
.Parameters.Add(New SqlParameter("@ItemCost", myRow.Item(6)))
.Parameters.Add(New SqlParameter("@ItemPrice", myRow.Item(7)))
.Parameters.Add(New SqlParameter("@Charge", myRow.Item(8)))
.Parameters.Add(New SqlParameter("@LastEditDate", FormatDateTime(Now, DateFormat.GeneralDate)))
.Parameters.Add(New SqlParameter("LastEditBy", mUser.UserName))
.Parameters.Add(New SqlParameter("@ID", myRow.Item(0)))
End With
myConn.Open()
myCMD.ExecuteNonQuery()
myConn.Close()
Next
lblStatus.Text = "Changes Saved"
lblStatus.Visible = True
Exit Sub
Problem:
Dim myMessage As String = Err.Description
lblStatus.Text = "Problem Saving Changes" & Err.Description
lblStatus.Visible = True
End Sub
ASKER
<%@ Page Language="C#"%>
<%@ Import Namespace = "System.Web" %>
<%@ Import Namespace = "System.Web.Security" %>
<%@ Import Namespace = "System.Security.Principal" %>
<%@ Import Namespace = "System.Runtime.InteropServices" %>
<script runat=server>
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext;
[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
public void Page_Load(Object s, EventArgs e)
{
if(impersonateValidUser("username", "domain", "password"))
{
//Insert your code that runs under the security context of a specific user here.
undoImpersonation();
}
else
{
//Your impersonation failed. Therefore, include a fail-safe mechanism here.
}
}
private bool impersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if(RevertToSelf())
{
if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if(DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if(token!= IntPtr.Zero)
CloseHandle(token);
if(tokenDuplicate!=IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}
private void undoImpersonation()
{
impersonationContext.Undo();
}
</script>
ASKER
The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications
TRUSTED BY