自定义登录不起作用

Custom sign in not working

我创建了一个用户可以注册,他的信息存储在数据库中。我现在单击登录,并输入用户凭据,这些将按应有的方式检索。但是系统不知道用户已经登录了,e.i.

if (Request.IsAuthenticated) 
    {
       userID = User.Identity.Name;  
    }

returns 为空,因为 Request.IsAuthenticated 为假。 这是我创建登录名时所做的:

一个。我将代码添加到 Web.config

<authentication mode="Forms">
  <forms loginUrl="~/Login/Login" timeout="2880" />
</authentication>

两个。我创建了验证用户的登录控制器:

[Authorize]
public class LoginController : Controller
{
    private RegisterUserController member = new RegisterUserController();

    [AllowAnonymous]
    [HttpPost]
    public ActionResult Login(LoginHandlerModel model)
    {

        if (ModelState.IsValid)
        {
            if (member.ValidateUser(model.userName, model.password, model.rememberUserOnThisComputer))
            {
                FormsAuthentication.SetAuthCookie(model.userName, model.rememberUserOnThisComputer);
                return RedirectToAction("OverView", "Home");
            }
            else
            {
                ModelState.AddModelError("", "Wrong email or password");
            }
        }

        ViewBag.WrongCredentials = "Wrong Credentials";

        return PartialView("_Login");
    }

三个。我在需要登录的操作中添加了一个过滤器:

[Authorize]
   public PartialViewResult Software()
    {
        ViewBag.Header = "Sofware";

        return PartialView("_Software");
    }

即使用户已经登录,也无法访问该Action。如果他没有登录,他不会被重定向到登录页面,我不知道为什么。

。我更改了我的代码以根据用户是否登录进行相应操作。这是无效的代码:

 @{
                var userID = "Guest";

                if (Request.IsAuthenticated) 
                {
                    userID = User.Identity.Name;             

                    <div style="margin-left: 3px; border: solid; border-color: #006BA0;">
                        <h5 style="color: green;">Welcome @User.Identity.Name (@Ajax.ActionLink("Log out", "LogOut", "Members", new AjaxOptions { UpdateTargetId = "updaterDiv" })) </h5>                              
                    </div>
                }
                else
                {
                    <div style="margin-left: 3px; border: solid; border-color: #006BA0;">
                        <h5 style="color: green;">
                            Welcome @userID (@Ajax.ActionLink("Log in", "LoginQuery", "Members", new AjaxOptions { UpdateTargetId = "updaterDiv" })
                            - @Ajax.ActionLink("Register", "RenderRegisterPage", "RegisterUser", new AjaxOptions { UpdateTargetId = "updaterDiv" }))
                        </h5>
                    </div>
                }

有什么我遗漏的吗?是否需要添加更多内容才能使其正常工作?

--------------------------------更新------------ ----------------------------

----------------------------第二次更新------------ ----------

问题是您使用了(我猜)VS2013 MVC5 WebApplication 模板,它利用 Identity 2.0 进行身份验证、授权和身份管理。

在web.config中有这个部分:

<system.webServer>
    <modules>
        <remove name="FormsAuthentication" />
    </modules>
</system.webServer>

这就是表单身份验证不处理您的 HTTP 请求的原因。

如果删除删除指令,应用程序将按预期运行。

否则,您可以摆脱 Forms Authentication 并学习如何使用 Identity(here IMO 的最佳起点)。