【发布时间】:2017-08-18 14:08:28
【问题描述】:
我正在使用 Asp.net core 1.1 和 Angular 4 开发应用程序。
在一个解决方案中,我有我的 API 和 Angular 应用程序。所以我不需要单独托管它们或启用 cors。要从客户端进行调用,我只需在 angular 的 http 方法中传递“/mycontroler”。
此应用的要求之一是使用使用 Azure AD 登录,但我找不到任何可以帮助我实现此流程的清晰且最新的内容。
到目前为止,我使用 Adal 从前端登录,我能够接收用户信息和令牌。那么,我现在如何在我的 api 中使用这个令牌来限制对资源的访问呢?
我的 API 启动代码是这样的:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404 &&
!Path.HasExtension(context.Request.Path.Value) &&
!context.Request.Path.Value.StartsWith("/api/"))
{
context.Request.Path = "/index.html";
await next();
}
});
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
context.Response.StatusCode = 500; // or another Status accordingly to Exception Type
context.Response.ContentType = "application/json";
var error = context.Features.Get<IExceptionHandlerFeature>();
if (error != null)
{
var ex = error.Error;
await context.Response.WriteAsync(ex.Message, Encoding.UTF8);
}
});
});
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
Authority = $"{Configuration["Authentication:AzureAd:AADInstance"]}{Configuration["Authentication:AzureAd:TenantId"]}",
Audience = Configuration["Authentication:AzureAd:ClientId"],
TokenValidationParameters =
new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
ValidIssuer =
$"{Configuration["Authentication:AzureAd:AADInstance"]}{Configuration["Authentication:AzureAd:TenantId"]}/v2.0"
}
});
app.UseMvcWithDefaultRoute();
app.UseDefaultFiles();
app.UseStaticFiles();
}
} }
但我不知道在我的控制器中具体做什么来检查用户权限和其他信息,例如他们所在的组。
在大多数情况下,我发现令牌是使用身份在 API 本身中发出的,但我的情况不同,因为令牌是由 AAD 发出。
我在这里阅读了很多文章和帖子,但找不到任何可以帮助我的东西。
【问题讨论】:
标签: angular asp.net-core azure-active-directory adal