I want to write a simple ASP.NET MVC based application that accepts the user's username and password via a form field, then authenticates to Office 365 and perform a simple Calendar lookup for that user. I do not need any fancy logic other than the above. I do not want to have the user redirected to any sites - I already have the user's Office 365 username/password.
My code so far - but it fails with an Object Reference not set on the first authContext.AquireTokenAsync call:
internal static async Task<OutlookServicesClient> EnsureOutlookServicesClientCreatedAsync(string capabilityName,
string username, string password)
{
var signInUserId = username;
AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.Authority,
new NaiveSessionCache(signInUserId));
try
{
DiscoveryClient discClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri,
async () =>
{
var authResult =
await
authContext.AcquireTokenAsync(SettingsHelper.DiscoveryServiceResourceId,
SettingsHelper.ClientId, new UserCredential(signInUserId, password));
return authResult.AccessToken;
});
var dcr = await discClient.DiscoverCapabilityAsync(capabilityName);
return new OutlookServicesClient(dcr.ServiceEndpointUri,
async () =>
{
var authResult = await authContext.AcquireTokenAsync(dcr.ServiceResourceId,
SettingsHelper.ClientId, new UserCredential(signInUserId, password));
return authResult.AccessToken;
});
}
catch (AdalException exception)
{
//Handle token acquisition failure
if (exception.ErrorCode == AdalError.FailedToAcquireTokenSilently)
{
authContext.TokenCache.Clear();
throw exception;
}
return null;
}
}
Can anyone advise how I should perform this authentication logic without having to redirect the user to some portal?