send Outlook Task programmatically from ASP.NET page
- From: Steve Moreno <SteveMoreno@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 12 Jan 2007 12:57:01 -0800
I have an asp.net application written in VB.NET that allows the user to
assign a task to other users. On my development laptop, everything works
fine. On the web server, it doesn't work. Below are the code and error
details:
I am enabling/disabling Impersonation on the fly with the following code:
Dim LOGON32_LOGON_INTERACTIVE As Integer = 2
Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
Dim impersonationContext As WindowsImpersonationContext
Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As
String, _
ByVal lpszDomain As String, _
ByVal lpszPassword As String, _
ByVal dwLogonType As Integer, _
ByVal dwLogonProvider As Integer, _
ByRef phToken As IntPtr) As Integer
Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _
ByVal ExistingTokenHandle As IntPtr, _
ByVal ImpersonationLevel As Integer, _
ByRef DuplicateTokenHandle As IntPtr) As Integer
Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As
IntPtr) As Long
Private Function impersonateValidUser(ByVal userName As String, ByVal
domain As String, ByVal password As String) As Boolean
Dim tempWindowsIdentity As WindowsIdentity
Dim token As IntPtr = IntPtr.Zero
Dim tokenDuplicate As IntPtr = IntPtr.Zero
impersonateValidUser = False
Try
If RevertToSelf() Then
If LogonUserA(userName, domain, password,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
tempWindowsIdentity = New
WindowsIdentity(tokenDuplicate)
impersonationContext =
tempWindowsIdentity.Impersonate()
If Not impersonationContext Is Nothing Then
impersonateValidUser = True
End If
End If
End If
End If
If Not tokenDuplicate.Equals(IntPtr.Zero) Then
CloseHandle(tokenDuplicate)
End If
If Not token.Equals(IntPtr.Zero) Then
CloseHandle(token)
End If
Catch ex As Exception
End Try
End Function
Private Sub undoImpersonation()
impersonationContext.Undo()
End Sub
And I call it like:
If impersonateValidUser("secretusername", "", "secretuserpassword") Then
SendTask(Request.QueryString("issueid"))
undoImpersonation()
End If
The "secretusername" is a local admin on the box and Outlook is installed on
the box and there is a profile created for that user. Since the user is a
local admin, it successfully processes the following line of code:
Dim app As Outlook.ApplicationClass = New Outlook.ApplicationClass
But it errors out with a "The operation failed." error at this line of code:
Dim tsk As Outlook.TaskItem =
CType(app.CreateItem(Outlook.OlItemType.olTaskItem), Outlook.TaskItem)
Here's the important details of the SendTask function I used above:
Dim app As Outlook.ApplicationClass = New
Outlook.ApplicationClass
Dim tsk As Outlook.TaskItem =
CType(app.CreateItem(Outlook.OlItemType.olTaskItem), Outlook.TaskItem)
tsk.Body = "You have been tasked with Issue Number 1"
tsk.StartDate = DateTime.Now
tsk.DueDate = DateTime.Now
tsk.Subject = "Item 1"
tsk.Recipients.Add("user@xxxxxxxx")
tsk.Assign()
tsk.Save()
tsk.Send()
app = Nothing
tsk = Nothing
I suspect it works fine on my development laptop since I'm running Outlook
whereas I am not running Outlook on the web box (but it is installed and
configured with a usuable profile).
I've been banging on this all week with no success. Does anyone out there
know what I'm doing wrong?
Please help. Thanks in advance,
Steve.
.
- Follow-Ups:
- Re: send Outlook Task programmatically from ASP.NET page
- From: Newbie Coder
- Re: send Outlook Task programmatically from ASP.NET page
- Prev by Date: xcopy deployment
- Next by Date: Re: xcopy deployment
- Previous by thread: xcopy deployment
- Next by thread: Re: send Outlook Task programmatically from ASP.NET page
- Index(es):
Relevant Pages
|