结合使用Microsoft Graph和ASP .NET Identity外部身份验证

时间:2019-03-21 12:15:26

标签: c# asp.net asp.net-mvc asp.net-identity microsoft-graph

我有一个使用ASP .NET Framework的Web应用程序。其功能之一是需要访问用户的Outlook日历。

目前,该应用程序使用常规的ASP .NET Identity身份验证,而没有OpenID。它还允许第三方身份验证提供程序。这是我的Startup类的代码:

public partial class Startup
{
    private static readonly string APP_ID = WebConfigurationManager.AppSettings["Site_Cert_App_ID"];
    private static readonly string APP_SECRET = WebConfigurationManager.AppSettings["Site_Cert_App_Secret"];

    // For more information on configuring authentication, please visit https://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context, user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        // Configure the sign in cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });            
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
        app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

        // Enables the application to remember the second login verification factor such as phone or email.
        // Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
        // This is similar to the RememberMe option when you log in.
        app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

        app.UseMicrosoftAccountAuthentication(new Microsoft.Owin.Security.MicrosoftAccount.MicrosoftAccountAuthenticationOptions
        {
            ClientId = APP_ID,
            ClientSecret = APP_SECRET,
            Scope = { "offline_access", "User.Read", "Calendars.Read" }
        });
    }
}

由于该应用程序的其他功能与Microsoft的其他服务完全不相关,因此我想保留此身份验证方法,并使用Microsoft帐户外部登录名来使用Graph API,而不是完整的OpendID身份验证过程。但是,我还没有找到任何示例,也无法弄清楚如何访问用于外部身份验证的令牌以将其重新用于Graph API。

有一种简单的方法可以做到吗?还是不可能,我必须执行整个流程才能代表用户获得令牌?

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:https://docs.microsoft.com/en-us/graph/auth-v2-user 步骤如下:

  • 使用Azure AD注册您的应用。
  • 获得授权。
  • 获取访问令牌。
  • 使用访问令牌调用Microsoft Graph。
  • 使用刷新令牌来获取新的访问令牌。

旁注,要获取特定用户的日历,您可以使用

GET /users/{id | userPrincipalName}/calendars

Documentation MS Graph

Graph Explorer