Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET MVC 4中设置登录会话?_C#_Asp.net_.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 如何在ASP.NET MVC 4中设置登录会话?

C# 如何在ASP.NET MVC 4中设置登录会话?,c#,asp.net,.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,.net,Asp.net Mvc,Asp.net Mvc 4,如何在MVC4中手动设置登录会话?我有一个方法可以检查用户是否有效,并向客户端返回true或false,但是我如何设置会话,以便具有[AUTHORIGHT]属性的方法在将来允许来自该用户的请求 [AllowAnonymous] [HttpPost] public bool Login(string userName, string password) { var isUserValid = SecurityManager.IsUserValid(u

如何在MVC4中手动设置登录会话?我有一个方法可以检查用户是否有效,并向客户端返回true或false,但是我如何设置会话,以便具有[AUTHORIGHT]属性的方法在将来允许来自该用户的请求

    [AllowAnonymous]
    [HttpPost]
    public bool Login(string userName, string password)
    {
        var isUserValid = SecurityManager.IsUserValid(userName, password);
        if (isUserValid )
            FormsAuthentication.SetAuthCookie(userName, true); 

        return isUserValid ;
    }

好的,检查您的web.config文件以获取FormAuthentication。例如:

<?xml version="1.0" encoding="utf-8"?>


<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
        <add  name="EFDbContext"  connectionString="Data  Source=(localdb)\v11.0;Initial
              Catalog=SportsStore;Integrated Security=True"
              providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="Email.WriteAsFile" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
    <globalization uiCulture="en-US" culture="en-US" />
    <authentication mode="Forms">
      <forms loginUrl="∼/Account/Login" timeout="2880" />
    </authentication>
  </system.web>
 </configuration>


您可以设置FormsAuthenticationCookie。这是在基本MVC应用程序的默认模板中提供的。好的,那么只需FormsAuthentication。SetAuthCookie(用户名,true)就可以了,对吗?这是怎么回事?我的客户是一个角度的应用程序,所以想知道这如何使未来的请求工作。浏览器上是否设置了cookie?服务器如何知道未来的请求来自同一个人?也许名称SetAuth COOKIE可以提供一个线索,说明它是否设置了COOKIE?cookie就是这样工作的,它们将与未来的请求一起发送,直到过期。@ErikFunkenbusch尝试设置验证cookie,但请求仍然无法访问标记为[Authorize]的方法。我已经更新了我的代码。这取决于Microsoft与您正在使用的新VS项目捆绑的各种身份验证系统。就是这样,谢谢。我必须在配置文件中指定模式。