我的ASP.NET Web应用程序存在这个问题,当我尝试创建登录时,它不会对用户进行身份验证。我有问题指出我的问题是在我的web.config文件中:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="Workice" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<authentication mode="Forms">
<forms loginUrl="~/Login/Login" timeout="2880" />
</authentication>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
上面的代码是完整的web.config文件。数据库工作和用户的验证也是完美的,所以它在web.config文件中。
这是我的控制者:
[HttpPost]
public ActionResult Login(Customer customer)
{
serviceSiteContext db = new serviceSiteContext();
var c = new Customer();
if (ModelState.IsValid)
{
if (IsValid(customer.EMail, customer.Pass))
{
using (var context = new serviceSiteContext())
{
c = context.Customer.Where(cos => cos.EMail == customer.EMail).FirstOrDefault();
}
FormsAuthentication.SetAuthCookie(c.Username, true);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", "Log ind data er ikke korrekte.");
}
}
return View(c);
}
答案 0 :(得分:2)
您的身份验证节点应该在system.web中,如下所示:
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Login/Login" timeout="2880" />
</authentication>
</system.web>