我正在尝试在现有的Azure云服务中实现Microsoft Identity。 Cloud Service由ASP.NET MVC 5 Web角色组成。 我正在使用UseOpenIdConnectAuthentication中间件。
应用程序成功将用户重定向到Microsoft登录页面。之后,应用程序只是提示返回应用程序的登录页面,用户没有登录。当我在AccountController中检查ExternalLoginCallback函数时,AuthenticationManager.GetExternalLoginInfoAsync()
的值返回null。 Startup.Auth.cs中的代码是:
public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = "https://login.microsoft.com/common/v2.0",
ClientId = "-----client id is here------",
Scope = "openid email",
RedirectUri = "https://localhost",
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false
}
}
);
}
我检查了OnAuthorizationCodeReceived
事件中的令牌是否可用,确实如此。 notification.JwtSecurityToken
包含所请求的信息。
有人知道GetExternalLoginInfoAsync()返回空值的原因,而应用程序实际收到了令牌吗?
答案 0 :(得分:0)
如果我理解正确,您使用OpenID connect OWIN组件将应用程序与Azure AD V2.0端点集成。
以下是我测试过的步骤,对我来说效果很好,供您参考:
注册一个网络应用程序2。使用Visual Studio
创建MVC应用程序 3。安装OpenId connect OWIN组件
Install-Package Microsoft.owin.security.openidconnect
4。修改Startup
类以使用openId connect
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = "https://login.microsoft.com/common/v2.0",
ClientId = "",
ClientSecret= "",
Scope = "openid email",
RedirectUri = "http://localhost:1317/",
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false
}
}
);
与您的代码之间的唯一区别是我注册了一个Web应用程序。所以代码中有ClientSecret
个参数。
答案 1 :(得分:0)
我终于找到了问题的解决方案。在Katana中存在某种错误,其中cookie被神秘地覆盖。
我已使用Kentor OwinCookieSaver nuget包来解决此问题。
我已将其置于所有Cookie身份验证属性之上,例如app.UseCookieAuthentication。您放置Owin中间件的顺序有意义。我不知道这是否是最好的订单,但这对我有用!