简体   繁体   中英

Exchange 401 Unauthorized

I am trying to connect to our exchange 2007 server. I have placed lots of exception handling to catch any errors and put them in the application log. Firstly, I have a function which ensures that a user can access the exchange service with the provided credentials:

public bool Logon()
{
    string pwd = /*Get password*/;

    try
    {
        service.Credentials = new WebCredentials(
            username + "@our.domain", pwd);

        service.FindItems(WellKnownFolderName.Outbox, new ItemView(1));
    }
    catch (Exception)
    {
        return false;
    }
    return true;
}

If this function returns false, an entry is placed in the application log reporting that the user failed to login, the process then terminates.

If the function succeeds, then somewhere down the track we call this function; it gets all appointments for the user which are starting in the next 10 minutes:

protected List GetFutureAppointments()
{
    try
    {
        SearchFilter.IsGreaterThanOrEqualTo startTime =
            new SearchFilter.IsGreaterThanOrEqualTo(
                AppointmentSchema.Start, DateTime.Now);

        SearchFilter.IsLessThanOrEqualTo endTime =
                new SearchFilter.IsLessThanOrEqualTo(
                    AppointmentSchema.Start, DateTime.Now.AddMinutes(10));

        SearchFilter filter = 
            new SearchFilter.SearchFilterCollection(LogicalOperator.And,
                new SearchFilter[] { startTime, endTime });

        FindItemsResults results =  
            service.FindItems(
                WellKnownFolderName.Calendar, filter, new ItemView(10));

        return new List(results.Items);
    }
    catch (Exception e)
    {
        Utilities.LogException(e);
        return null;
    }
}

As you can see, the function will catch all exceptions and log them. Showing up in the log is Request failed. The remote server returned an error: (401) Unauthorized. Request failed. The remote server returned an error: (401) Unauthorized. The stack trace points to the service.FindItems() function.

So I'm a little confused and probably don't know enough about exchange or web services or whatever. The logon function is returning true, but then authorization fails later on. Any suggestions?

您的凭据有可能使您可以访问发件箱,但不能访问日历?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM