Ben*_*tra 5 asp.net-web-api angularjs owin adal openid-connect
我找到了一个模板,可以通过 AzureAD 为 MultiTenant Web 应用程序连接 Office365 服务。
这很好,但是这个示例是用 ASP.NET MVC 编写的,我想修改它并使其作为带有 ASP.NET WebAPI2 的 Angular SPA 工作。
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = SettingsHelper.ClientId,
Authority = SettingsHelper.Authority,
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
{
ValidateIssuer = false
},
Notifications = new OpenIdConnectAuthenticationNotifications()
{
AuthorizationCodeReceived = (context) =>
{
var code = context.Code;
ClientCredential credential = new ClientCredential(SettingsHelper.ClientId, SettingsHelper.AppKey);
string tenantID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
string signInUserId = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
AuthenticationContext authContext = new AuthenticationContext(string.Format("{0}/{1}", SettingsHelper.AuthorizationUri, tenantID), new ADALTokenCache(signInUserId));he
AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, SettingsHelper.AADGraphResourceId);
return Task.FromResult(0);
},
RedirectToIdentityProvider = (context) =>
{
string appBaseUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase;
context.ProtocolMessage.RedirectUri = appBaseUrl + "/";
context.ProtocolMessage.PostLogoutRedirectUri = appBaseUrl;
return Task.FromResult(0);
},
AuthenticationFailed = (context) =>
{
context.HandleResponse();
return Task.FromResult(0);
}
}
});
Run Code Online (Sandbox Code Playgroud)
}
AccountController 中的SignIn方法是通过 ASP.NET MVC Action.Link调用的
public class AccountController : Controller
{
public void SignIn()
{
if (!Request.IsAuthenticated)
{
HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
}
}
Run Code Online (Sandbox Code Playgroud)
所述的AccountController是MVC控制器和方法.Challenge所述的IAuthenticationManager实际上使302重定向到与适当的OAuth2 URL参数的权限网址。如果我们在ApiController 中重用相同的代码并通过XHR调用它,它将不起作用,它将 OpenId cookie 返回给客户端并且显然不会重定向。
什么是ApiController的有效SignIn方法?
| 归档时间: |
|
| 查看次数: |
987 次 |
| 最近记录: |