ASP.NET MVC-自定义身份验证不起作用

时间:2018-11-23 08:04:40

标签: asp.net-mvc authentication forms-authentication

我创建了一个简单的身份验证表单,用于使用FormsAuthentication对用户进行身份验证。

这就是我的使用方式

public ActionResult LoginUser(Login login)
        {
            //var encodedPassword = HashPassword.Decode(login.Password);
            var encodedPassword = login.Password;
            var loginData = context.Accounts.Where(p => p.Username == login.Username && p.Password == encodedPassword).SingleOrDefault();

            if (loginData != null)
            {
                FormsAuthentication.SetAuthCookie(login.Username, false);


                return RedirectToAction("Index", "Home");
            }
            else
            {
                TempData["errMess"] = "Invalid credential";
                return RedirectToAction("Login");
            }
        }

现在我有一个代码来获取_layout.cshtml中的登录用户名

<div class="profile_info">
   <span>
      Welcome,
      @if (Request.IsAuthenticated)
      {
         @Html.Encode(User.Identity.Name)
      }else {
         <strong>Hello worlds</strong>
      }
    </span>
</div>

但这不能解决我的问题 我做错了什么或我想念什么,请帮助我,我是dot net的新手 谢谢!

1 个答案:

答案 0 :(得分:1)

您添加了

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

<system.web>的{​​{1}}部分里面