MVC5:User.Identity.IsAuthenticated始终返回true

时间:2014-09-16 16:34:22

标签: authentication asp.net-mvc-5 owin

我们公司有SSO网站,如果用户未经过身份验证,我们的所有应用程序都会被重定向。 SSO站点使用Forms身份验证对用户进行身份验证。它一直在为所有应用程序工作。(ASP.NET应用程序)

现在我们使用VS 2013创建了新的MVC5应用程序。我正在尝试使用Forms身份验证。如果用户未经过身份验证,我想将用户重定向到登录URL(SSO站点)。以下是我的代码。但是当我调试时,用户总是经过身份验证。 IsAutheticated属性为true,AuthenticationType为“Negotiate”,Identity为“Windows”(即使在配置文件中为“Forms”)

(注意我在VS中使用IIS express进行调试,如果这样做有所不同。另外它是MVC 5应用程序,是因为OWIN。我怎么知道?)

<system.web>
   <compilation debug="true" targetFramework="4.5" />
 <authentication mode="Forms" >
   <forms loginUrl="/Account/Login"></forms>
 </authentication>
 <authorization>
  <deny users="?" />
 </authorization>

public class AccountController : Controller
 {
   public ActionResult Login()
   {
      string loginUrl = AppSettings.Authentication.LoginUrl;
      string failOverUrl = AppSettings.Authentication.FailoverLoginUrl;
     string securityGroup = AppSettings.Authentication.SecurityGroup;

     if (!User.Identity.IsAuthenticated) // IsAutheticated is always true, why?
     {
        var returnUrl = "someresturnurl";
        MyAuthenticator.Authenticate(loginUrl, failOverUrl, returnUrl, securityGroup);
     }
     else
    {
        // Redirect the user if they are already authenticated.
       return RedirectToAction("Index", "Home");
    }
  }
}

1 个答案:

答案 0 :(得分:0)

在ASP.NET MVC5中,身份验证机制发生了重大变化。

可能所有身份验证配置都来自OWIN Startup class

Here is a link where you can find how this configuration may look like

HttpListener listener = (HttpListener)app.Properties["System.Net.HttpListener"];
listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;

这是一个关于ASP.NET identity basics

的好主题

我希望这会有所帮助。

相关问题