如何使用 Azure AD 授权代码流?
我正在构建一个 ASP.NET (v4.8) Web 应用程序,该应用程序将作为 Azure 应用服务托管,但目前我们位于本地主机上。
我已成功配置 Azure AD,并且收到授权代码,因为我将应用服务配置为发送访问令牌。应用程序注册仅具有User.Read
(委派)权限。
在我的 Startup.cs
文件中,我配置了 OpenIdConnectAuthenticationNotifications
,以便接收访问代码
在已收到授权代码
。这是代码:
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType( CookieAuthenticationDefaults.AuthenticationType );
app.UseCookieAuthentication(new CookieAuthenticationOptions());
authority = aadInstance + tenantId;
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions {
ClientId = clientId, Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
TokenResponseReceived = (tr) => { return Task.FromResult(0); },
AuthorizationCodeReceived = (code) => {
// you are here! what's next?
access_code = code.Code;
return Task.FromResult(0);
},
SecurityTokenReceived = (token) =>
{
return Task.FromResult(0);
},
AuthenticationFailed = (context) => { return System.Threading.Tasks.Task.FromResult(0); }
}
});
app.UseStageMarker(PipelineStage.Authenticate);
}
我的目标是作为当前用户调用此图形端点来获取他们的 JobTitle 和 >来自 Azure AD 的部门。这是资源:https://graph.microsoft.com/v1.0/me
我正在关注这个文档,但尚不清楚如何处理提供的 access_code
。请帮助我理解。
- 这是
access_code
不记名令牌吗?我可以直接使用它来调用图形API吗? - 我是否必须使用它来调用 /token 端点才能获取不记名令牌?
- 我是否必须使用它来调用 /authorize 端点才能获取不记名令牌?
- 我现在正在发出直接 HTTP 请求,我应该使用 MSAL 还是 Graph SDK?
我想我正在尝试完成这一步:
这是我当前正在处理的代码,它返回 HTTP CODE 400
(错误请求):
private void GetOtherProfileData()
{
var cId = Startup.clientId;
var tenantId = Startup.tenantId;
var scope = Startup.scope;
// scope: https%3A%2F%2Fgraph.microsoft.com%2Fuser.read
var code = Startup.access_code;
var redir = HttpUtility.UrlEncode(Startup.redirectUri);
var req_url = $@"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token?client_id={cId}&scope={scope}
&code={code}&redirect_uri={redir}&grant_type=authorization_code
&code_verifier=ThisIsntRandomButItNeedsToBe43CharactersLong";
var req = WebRequest.CreateHttp(req_url);
req.Method = "POST";
req.ContentLength = 0;
req.ContentType = "application/x-www-form-urlencoded";
var resp = req.GetResponse();
var str = resp.GetResponseStream();
var json = new StreamReader(str).ReadToEnd();
Trace.TraceInformation(json);
/// this should return bearer token and then we go call the /me endpoint...
///right?
}
任何代码示例或指向最近的文档会有所帮助。
I am building an ASP.NET (v4.8) Web application that will be hosted as an Azure App Service, but for now we are on localhost.
I am configured for Azure AD successfully and I am receiving an authorization code because I configured my app service to send the access token. The app registration has ONLY User.Read
(delegated) permissions.
In my Startup.cs
file, I've configured OpenIdConnectAuthenticationNotifications
so that I am receiving the access code
in AuthorizationCodeReceived
. Here is the code:
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType( CookieAuthenticationDefaults.AuthenticationType );
app.UseCookieAuthentication(new CookieAuthenticationOptions());
authority = aadInstance + tenantId;
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions {
ClientId = clientId, Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
TokenResponseReceived = (tr) => { return Task.FromResult(0); },
AuthorizationCodeReceived = (code) => {
// you are here! what's next?
access_code = code.Code;
return Task.FromResult(0);
},
SecurityTokenReceived = (token) =>
{
return Task.FromResult(0);
},
AuthenticationFailed = (context) => { return System.Threading.Tasks.Task.FromResult(0); }
}
});
app.UseStageMarker(PipelineStage.Authenticate);
}
My objective is to call this graph endpoint as the current user to get their JobTitle and > Department from Azure AD. Here is the resource: https://graph.microsoft.com/v1.0/me
I was following this documentation, but it was not clear what to do with the provided access_code
. Please help me understand.
- Is this
access_code
a bearer token? can I use it directly to call the graph API? - Do I have to use it to call the /token endpoint to get a bearer token?
- Do I have to use it to call the /authorize endpoint to get a bearer token?
- I am making direct HTTP requests now, should I use MSAL or Graph SDK?
I think I am trying to accomplish this step:
This is the code I am currently working on, and it returns HTTP CODE 400
(Bad Request):
private void GetOtherProfileData()
{
var cId = Startup.clientId;
var tenantId = Startup.tenantId;
var scope = Startup.scope;
// scope: https%3A%2F%2Fgraph.microsoft.com%2Fuser.read
var code = Startup.access_code;
var redir = HttpUtility.UrlEncode(Startup.redirectUri);
var req_url = $@"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token?client_id={cId}&scope={scope}
&code={code}&redirect_uri={redir}&grant_type=authorization_code
&code_verifier=ThisIsntRandomButItNeedsToBe43CharactersLong";
var req = WebRequest.CreateHttp(req_url);
req.Method = "POST";
req.ContentLength = 0;
req.ContentType = "application/x-www-form-urlencoded";
var resp = req.GetResponse();
var str = resp.GetResponseStream();
var json = new StreamReader(str).ReadToEnd();
Trace.TraceInformation(json);
/// this should return bearer token and then we go call the /me endpoint...
///right?
}
Any code samples or pointers to recent documentation would be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个 access_code 是不记名令牌吗?我可以直接使用它来调用图形API吗?
我是否必须使用它来调用 /token 端点才能获取不记名令牌?
我是否必须使用它来调用 /authorize 端点才能获取不记名令牌?
我现在正在发出直接 HTTP 请求,我应该使用 MSAL 还是 Graph SDK?
Is this access_code a bearer token? can I use it directly to call the graph API?
Do I have to use it to call the /token endpoint to get a bearer token?
Do I have to use it to call the /authorize endpoint to get a bearer token?
I am making direct HTTP requests now, should I use MSAL or Graph SDK?
我在这里找到了一个很好的示例: https://github.com/Azure -Samples/ms-identity-aspnet-webapp-openidconnect
我试图在没有
客户端密码
的情况下执行此操作,这是一个错误。这就是我实现它的方式=>
这是
BuildConfidentialClientApplication
的实现内部的内容:I found a good sample here: https://github.com/Azure-Samples/ms-identity-aspnet-webapp-openidconnect
I was trying to do this without a
client secret
, that was a mistake.This is how I implemented it =>
This is what is inside the implementation of
BuildConfidentialClientApplication
: