c# - 登录 Microsoft AD 后 Request.IsAuthenticated 始终为 false

标签 c# 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广告门户注册。但在这个特定的项目中,登录微软后,request.isauthentciated 始终为 false。但是,我从 SecurityTokenValidated 的上下文参数中获得了所有正确的声明。您知道为什么会发生这种情况吗?因为我将项目设置为MVC/WebAPI? 家庭 Controller :

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

启动.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);
                        }
                    }
                }
            );

谢谢您并致以亲切的问候!

最佳答案

当您处理 <authentication mode="Forms"> 时,Request.IsAuthenticated 始终为 FALSE如果您处理 <authentication mode="Windows" /> 则始终为 TRUE 。请仔细检查您的 web.config 并删除此设置。之后,Request.IsAuthenticated 现在在您的 AzureAD 上受到保护。

关于c# - 登录 Microsoft AD 后 Request.IsAuthenticated 始终为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52882048/

相关文章:

python-3.x - 无法在 python 中迭代 Azure ADGroupPaged 对象

asp.net - 在单个 Azure 云服务上部署多个 Web 角色和辅助角色

asp.net-mvc - Base64编码的JsonResult

asp.net-mvc - 从 Controller 传递 json 到 View

c# - AzureStorageFile.Uri 未返回具有特殊字 rune 件名的正确 Uri,导致 404 响应

c# - 隐藏父类的实现细节

c# - Monotouch Binding 项目构建错误

c# - 如何找到两种类型中最小的可分配类型(重复)?

c# - 如何使用 3 includes 优化 Entity Framework 查询

c# - Parallel.ForEach 错误 HttpContext.Current