2017-06-02 105 views
1

让我首先说这是我的第一个.net核心应用程序,因此我可能会在这里以一个白痴的身份出现。一般来说,我也是新的代币。从asp.net核心获取Microsoft Graph的访问令牌认证Web应用程序

这么说,我有一个应用程序,它验证对Azure的AD用户像这样:

Startup.cs

using Microsoft.AspNetCore.Authentication.Cookies; 

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddMvc(); 

    services.AddAuthentication(
     SharedOptions => SharedOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme); 

    services.AddAuthorization(options => 
    { 
     //Auth Policies using group claims 
    }); 
} 
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    //loggerFactory 

    app.UseCookieAuthentication(); 

    app.UseOpenIdConnectAtuhentication(new OpenIdConnectOptions 
    { 
     ClientID = Configuration["Authentication:AzureAd:ClientId"], 
     Authority = Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"], 
     CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"] 
    }); 

    //Mvc Routing 
} 

AccountController.cs

[HttpGet] 
public IActionResult SignIn() 
{ 
    return Challenge(
     new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectDefaults.AuthenticationScheme); 
} 

在控制器我要保护然后我用[Authorize(Policy = "Whatever")]

所有这些都是工作g reat,所以我的问题是我已经拥有的cookie是否包含我需要的令牌,如果是这样,我可以简单地使用User.Claims来访问它,还是需要使用IAuthenticationProvider来设置它的示例。 net 4.6 Here

如果是后者,我如何在没有使用Owin的情况下如此操作?

我应该补充说,虽然授权工作正常,但现在我想要做的事情包括像列出OU中的所有用户这样的事情。据我所知,我必须访问Microsoft Graph。也许,我正在走向完全错误的方向?

+0

相关:https://stackoverflow.com/questions/41519132/how-to-store-the-token-received-in-acquiretokenasync-with-active-directory –

回答

3

您可以使用ADAL获取访问令牌。你可以在这里找到一个很好的示例应用程序:https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-openidconnect-aspnetcore

以下是检索令牌特别重要的部分:https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-openidconnect-aspnetcore/blob/master/WebApp-WebAPI-OpenIdConnect-DotNet/Startup.cs#L100

private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext context) 
    { 
     // Acquire a Token for the Graph API and cache it using ADAL. In the TodoListController, we'll use the cache to acquire a token to the Todo List API 
     string userObjectId = (context.Ticket.Principal.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value; 
     ClientCredential clientCred = new ClientCredential(ClientId, ClientSecret); 
     AuthenticationContext authContext = new AuthenticationContext(Authority, new NaiveSessionCache(userObjectId, context.HttpContext.Session)); 
     AuthenticationResult authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(
      context.ProtocolMessage.Code, new Uri(context.Properties.Items[OpenIdConnectDefaults.RedirectUriForCodePropertiesKey]), clientCred, GraphResourceId); 

     // Notify the OIDC middleware that we already took care of code redemption. 
     context.HandleCodeRedemption(); 
    } 

您从Azure AD获得授权码,您需要将授权码交换为要访问的API的访问令牌或令牌。对于Microsoft Graph,请确保将资源URI设置为https://graph.microsoft.com(该示例针对Azure AD Graph API)。

然后,您可以调用API,如下所示:https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-openidconnect-aspnetcore/blob/master/WebApp-WebAPI-OpenIdConnect-DotNet/Controllers/TodoController.cs#L31

  string userObjectID = (User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value; 
      AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID, HttpContext.Session)); 
      ClientCredential credential = new ClientCredential(Startup.ClientId, Startup.ClientSecret); 
      result = await authContext.AcquireTokenSilentAsync(Startup.TodoListResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId)); 

result包含您需要连接到HTTP请求的访问令牌。它将从会话缓存中静默检索。

我想说明的是,在ASP.NET Core 2.0中,整个过程要简单得多。那里的Open Id Connect身份验证处理程序实际上可以为您检索令牌。

+0

谢谢!我发誓,我到处搜寻,正是因为这一点。大声笑。我没有使用大量的软件包,所以如果我的支持没有太多的额外工作,我会跳到2.0。 –

+0

2.0仍然处于预览状态,因此您可以获得出血边缘的所有问题:) – juunas

相关问题