我有一个ASP.NET 4.5.1 MVC/WebAPI项目。它使用SSO认证Microsoft。成功登录Microsoft后,我的HomeController仍然有Request.IsAuthenticated。我在web config文件中注册的URL是http://localhost:58686/。
现在,我有一个成功登录后的示例MVC应用程序。我已经反复检查了代码,我的MVC/Web Api项目使用相同的startup.cs、相同的web config结构和相同的注册到Azure AD Portal。但在这个特定的项目中,登录Microsoft后,request.isauthentciated始终为false。然而,我从SecurityTokenValidated的上下文参数中获得了所有正确的声明。您有任何关于为什么会发生这种情况的想法吗?因为我将项目设置为MVC/WebAPI?
HomeController:
现在,我有一个成功登录后的示例MVC应用程序。我已经反复检查了代码,我的MVC/Web Api项目使用相同的startup.cs、相同的web config结构和相同的注册到Azure AD Portal。但在这个特定的项目中,登录Microsoft后,request.isauthentciated始终为false。然而,我从SecurityTokenValidated的上下文参数中获得了所有正确的声明。您有任何关于为什么会发生这种情况的想法吗?因为我将项目设置为MVC/WebAPI?
HomeController:
public ActionResult Index()
{
//var y = HttpContext.User.Identity.IsAuthenticated;
if (!Request.IsAuthenticated)
{
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties { RedirectUri = System.Configuration.ConfigurationManager.AppSettings["redirectUrl"] },
OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
Startup.cs
public void Configuration(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
RedirectUri = redirectUrl,
PostLogoutRedirectUri = redirectUrl,
Scope = OpenIdConnectScopes.OpenIdProfile,
ResponseType = OpenIdConnectResponseTypes.IdToken,
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters() { ValidateIssuer = false },
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = OnAuthenticationFailed,
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);
},
SecurityTokenValidated = (context) =>
{
var identity = context.AuthenticationTicket.Identity;
return Task.FromResult(0);
}
}
}
);
谢谢您,祝好!