你能告诉我为什么FormsAuthentication.SetAuthCookie(user.Name, false);
不是Request.IsAuthenticated
真的吗?
这是我的代码:
[HttpPost]
public ActionResult LogIn(karcioszki.Models.UserLoginModel user)
{
if (ModelState.IsValid)
{
if (IsValid(user.Name, user.Password))
{
FormsAuthentication.SetAuthCookie(user.Name, false);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", "Login or password is incorrect");
}
}
return View(user);
}
Run Code Online (Sandbox Code Playgroud)
如果声明:
@if (Request.IsAuthenticated)
{
<a href="@Href("~/")" class="active">Home</a>
<a href="@Href("~/Cards")">Cards</a>
@Html.ActionLink("Log out", "Logout", "User")
@Html.Encode(User.Identity.Name)
}
Run Code Online (Sandbox Code Playgroud)
另外,请告诉我,如何使它工作?
编辑:我在web.config(两者)中添加了身份验证,但它仍然无法正常工作.
<system.web>
<httpRuntime targetFramework="4.5" />
<compilation debug="true" targetFramework="4.5" />
<authentication mode="Windows"/>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" /> …
Run Code Online (Sandbox Code Playgroud) 我尝试连接:Microsoft SQL Server数据库文件(SqlClient),但我收到错误:
尝试连接到数据库失败,并显示以下信息:与SQL Server建立连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称是否正确,以及SQL Server是否配置为允许远程连接.(提供程序:SQL网络接口,错误:52 - 无法找到本地数据库运行时安装.验证是否已正确安装SQL Server Express并且已启用本地数据库运行时功能.)
所以,我在服务器资源管理器中单击连接到数据库
这个窗口出现了.我在其中选择:Microsoft SQL Server数据库文件(SqlClient)然后浏览我的.mdf数据库文件.
单击确定会出现前面提到的错误:
服务器正在运行,我在数据库中使用Windows身份验证.任何sugestions?