views:

1598

answers:

4

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?

+1  A: 

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

Otávio Décio
Unless I'm doing something horribly wrong, I've tried that too without success. In the IIS manager, went to application pools -> advanced settings on the pool -> Process Model -> Identity. It was using the NetworkService built-in account, but changed it to the custom account that has access.
Anjisan
did you restart iis after that?
Rob Fonseca-Ensor
@Rob - good point, it is a good idea to restart.
Otávio Décio
I did -- even did a iisreset on the command line to watch and make sure services stopped and restarted, but without success.
Anjisan
Got it, thanks for your help!
Anjisan
+1  A: 

I find IIS authentication eternally confusing.

Clearly, the problem is that the security context of your web application does not allow it to access your Outlook calendar. Unfortunately, there are several places where you can make adjustments, and they all need to play together. In order for someone else to figure out what goes wrong, you need to provide more information about your application (web config) and IIS settings.

Or, alternatively, take a look at this article, which I find very useful. Maybe this will help you figure it out yourself.

Try enabling impersonation, either in the web.config or in the code, turn off anonymous access, and try the Classic pipeline for your app pool if Integrated does not work.

cdonner
Thanks much! I find IIS authentication confusing too. Like you said, it ended up being a combination of things. Great link.
Anjisan
A: 

Ended up being something else entirely:

2 fold step -- (1) Enter identity credentials to impersonate either in web.config or through iis and (2) give write access to that same user in Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files

Editing pool settings has no effect as it turns out. I can go back and put the pool under NetworkService and it doesn't matter.

Thanks for your help! Much appreciated!! It definitely put me on the right track

Anjisan
A: 

I tried to coonect thru ASP.NET application to Outllook with Impersonation = TRUE to get the Shared Calendar appointments but failed. The code runs with A Console application. Problem arises when Outlook instance is created by NETWORK SERVICE when executed with ASP>NET application. Please suggest.

I might suggest you ask a separate question and then let me know. Would be a lot easier. Sounds like you have it working fine with console app, but not as an asp.net app. Is that right? Do you have <identity impersonate="true" userName="name" password="pass" /> with set username and pass?
Anjisan