C# 如何执行从MVC到Office 365 AD的简单身份验证

C# 如何执行从MVC到Office 365 AD的简单身份验证,c#,asp.net-mvc,asp.net-mvc-4,office365,C#,Asp.net Mvc,Asp.net Mvc 4,Office365,我想编写一个简单的基于ASP.NET MVC的应用程序,通过表单字段接受用户的用户名和密码,然后向Office 365进行身份验证,并为该用户执行简单的日历查找。除了上面提到的,我不需要任何花哨的逻辑。我不想让用户重定向到任何站点-我已经拥有该用户的Office 365用户名/密码 到目前为止我的代码-但由于未在第一个authContext.AquireTokenAsync调用上设置对象引用,因此失败: internal static async Task<OutlookServicesC

我想编写一个简单的基于ASP.NET MVC的应用程序,通过表单字段接受用户的用户名和密码,然后向Office 365进行身份验证,并为该用户执行简单的日历查找。除了上面提到的,我不需要任何花哨的逻辑。我不想让用户重定向到任何站点-我已经拥有该用户的Office 365用户名/密码

到目前为止我的代码-但由于未在第一个authContext.AquireTokenAsync调用上设置对象引用,因此失败:

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;
    }
}

有人能建议我如何执行此身份验证逻辑而不必将用户重定向到某个门户吗?

你不能。OutlookServicesClient需要OAuth流,该流让用户通过重定向登录到OAuth端点login.windows.net。您不必询问用户的用户名或密码,因为OAuth端点已经为您处理了所有这些