登录到Microsoft AD后,Request.IsAuthenticated始终为false

时间:2018-10-18 20:28:29

标签: .net asp.net-mvc azure single-sign-on

我有一个ASP.NET 4.5.1 MVC / WebAPI项目。它使用SSO对Microsoft进行身份验证。成功登录到Microsoft后,我的HomeController上仍然有Request.IsAuthenticated。我已注册到Web配置文件的URL是http://localhost:58686/

现在,我有一个仅MVC的示例应用程序,该应用程序在登录后可以成功进行身份验证。我已经对代码进行了两次,三次检查,并且我的mvc / Web Api项目使用相同的startup.cs,相同的Web配置结构和相同的注册来访问Azure广告门户。但是在这个特定项目中,登录到Microsoft后,request.isauthentciated始终为false。但是,我从SecurityTokenValidated的context参数获得了所有正确的声明。您是否知道为什么会发生?因为我将项目设置为MVC / WebAPI? 家用控制器:

  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);
                        }
                    }
                }
            );

谢谢您和亲切的问候!

1 个答案:

答案 0 :(得分:0)

Request.IsAuthenticated在处理<authentication mode="Forms">时始终为FALSE,而在处理<authentication mode="Windows" />时始终为TRUE。请仔细检查您的web.config并删除此设置。之后,Request.IsAuthenticated现在可以在您的AzureAD上防御。