【发布时间】:2015-07-17 00:44:59
【问题描述】:
我正在尝试通过 Azure AD 应用程序角色创建受保护的控制器。
这里是 Startup.Auth 的一个豁免,它基本上是由 Visual Studio 模板提供的:
public void ConfigureAuth(IAppBuilder app)
{
ApplicationDbContext db = new ApplicationDbContext();
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = Authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
// If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
AuthorizationCodeReceived = (context) =>
{
var code = context.Code;
ClientCredential credential = new ClientCredential(clientId, appKey);
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
AuthenticationContext authContext = new AuthenticationContext(Authority, new ADALTokenCache(signedInUserID));
AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);
return Task.FromResult(0);
}
}
});
}
尝试过具有以下属性的 ApiController:
[Authorize(Roles = "Administrators")]
// GET: api/Questions
[ResponseType(typeof(Question))]
public IHttpActionResult GetQuestions()
{
....
}
和一个 MVC 控制器:
[Authorize(Roles = "Administrators")]
public ActionResult Index()
{
....
}
在 Azure 应用程序清单中定义了以下内容:
"appRoles": [
{
"id": "B4531A9A-0DC8-4015-8CE5-CA1DA1D73754",
"allowedMemberTypes": ["User"],
"description": "Administrators",
"displayName": "Administrators",
"value": "Administrators",
"isEnabled": true,
"origin": "Application"
}
]
现在执行/api/Questions 的GET 请求重定向到https://login.microsoftonline.com 并且用户身份验证似乎成功了,此外localhost 和microsoft online 之间存在无限循环的请求。见下文:
我做错了什么?
使用 [Authorize] 效果很好。
【问题讨论】:
-
你的无限重定向可能是这样的:coding.abel.nu/2014/11/…
标签: asp.net-mvc asp.net-web-api asp.net-web-api2 azure-active-directory openid-connect