C# – Cannot connect ASP.NET to Outlook in IIS, but can through Visual Studio

ciisoutlookpermissions

I'm doing a home project that started off really easy (doesn't that always happen?) and then took a nasty permissions turn.

Basically, I have a home intranet and my PC is doing double-duty as the home web server. I'm running Vista, so we're talking IIS 7.

In Visual Studio, this works perfectly. I have my homepage query Outlook (2007) and display the next couple appointments. I do this as so,

using Microsoft.Office.Interop.Outlook;

//Be kind -- this is a work in progress
public static string nextAppointment()
{
    System.Text.StringBuilder returnString = new System.Text.StringBuilder();

        try
        {
            Application outlookApp = new ApplicationClass();
            NameSpace outlookNamespace = outlookApp.GetNamespace("MAPI");
            MAPIFolder theAppts = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);

            List<AppointmentItem> todaysAppointments = new List<AppointmentItem>();
            TimeSpan oneday = new TimeSpan(24, 0, 0);
            DateTime today = DateTime.Today;
            DateTime yesterday = today.Subtract(oneday);
            DateTime tomorrow = today.Add(oneday);

            foreach (AppointmentItem someAppt in theAppts.Items)
            {
                if (someAppt.Start > yesterday && someAppt.Start < tomorrow)
                {
                    todaysAppointments.Add(someAppt);
                }
            }

            foreach (AppointmentItem todayAppts in todaysAppointments)
            {
                returnString.Append(todayAppts.Start.ToShortTimeString() + " -- " + todayAppts.Subject + "<br />");
            }
        }
        catch (System.Exception ex)
        {
            //TO-DO: Add some real handling
            returnString.Append("Cannot access calendar");
        }


        return returnString.ToString();
}

This code snippet is just a work in progress, but you get the idea. It looks to see what kind of calendar events I have within a 24 hr period and then adds them to a string that I eventually write-out on the webpage. When I debug this in Visual Studio, it runs great through the ASP.NET Development Studio. Feeling confident, I take the same code and run it on IIS for all at home to enjoy, and I get the error (when I don't catch my exception),

Retrieving the COM class factory for
component with CLSID
{0006F03A-0000-0000-C000-000000000046}
failed due to the following error:
80070005.

Description: An unhandled exception
occurred during the execution of the
current web request. Please review the
stack trace for more information about
the error and where it originated in
the code.

Exception Details:
System.UnauthorizedAccessException:
Retrieving the COM class factory for
component with CLSID
{0006F03A-0000-0000-C000-000000000046}
failed due to the following error:
80070005.

ASP.NET is not authorized to access
the requested resource. Consider
granting access rights to the resource
to the ASP.NET request identity.
ASP.NET has a base process identity
(typically {MACHINE}\ASPNET on IIS 5
or Network Service on IIS 6) that is
used if the application is not
impersonating. If the application is
impersonating via , the identity
will be the anonymous user (typically
IUSR_MACHINENAME) or the authenticated
request user.

To grant ASP.NET access to a file,
right-click the file in Explorer,
choose "Properties" and select the
Security tab. Click "Add" to add the
appropriate user or group. Highlight
the ASP.NET account, and check the
boxes for the desired access.

I've tried altering permissions through Explorer on the site but no luck.

Any ideas?

Best Answer

Try running the web app's application pool under a user that has access (such as a domain user).

Related Topic