Chris Hadfield
Chris Hadfield

Reputation: 524

ASP.NET MVC - Custom authentication doen't work

I have created a simple authentication form to authenticate user using FormsAuthentication.

This is how I have used it

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");
            }
        }

Now I have a code to get a logged in username in _layout.cshtml

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

but this doesn't solve my problem What am I doing wrong or am I missing something please help me I am new in dot net Thanks!

Upvotes: 0

Views: 72

Answers (1)

Nishan Dhungana
Nishan Dhungana

Reputation: 871

Did you add

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

inside <system.web> section in web.config

Upvotes: 1

Related Questions