Forms Authentication with Razor not working

I am trying to get FormsAuthentication to work with my Razor application (MVC 3). I have a LoginController that calls my LoginPage (which is in Views / Shared); my web.config has the LoginUrl parameter set to "/ Login /". When an application tries to open the main page, the [Authorize] line will correctly return LoginPage, but where problems arise.

Here is my LoginController.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ABMCEditAndReports.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Index()
        {
            return View("LoginPage");
        }

    }
}

Here is my LoginPage.cshtml:

@{
    ViewBag.Title = "Login Page";
    ViewBag.Header = "Login Page";
}

<script type="text/javascript">
    $.ajaxSetup({ cache: false });

    function onClickLogin() {       
        if (Membership.ValidateUser(txtUsername.Text, txtPassword.Text)) {
           FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, false);
        }
        else {
           lblErrorMessage.Text = "This works better if you use YOUR user name and password";
        }
    }
</script>

<div style="text-align:center;">
<h2>Login Required</h2>
User Name:
<input type="text" name="txtUsername" />
<br />
Password:
<input type="password" name="txtPassword" />
<br />
<input type="button" id="btnLogin" value="Login" onclick="onClickLogin()" />
<br />
<label id="lblErrorMessage" style="color:Red"></label>
</div>

The first problem is that when the application starts, VS stops at the $ .ajaxSetup line with "Microsoft JScript runtime error:" $ "is undefined".

, , "", Membership.ValidateUser " Microsoft JScript:" "- undefined".

, $.ajaxSetup .

"@using System.Web.Security" LoginPage.cshtml System.Web.Security web.config( , /); .

? ValidateUser .cs? , RedirectFromLoginPage? ( $.ajaxSetup?)

+1
1

FormsAuthentication ASP.NET MVC, . :

:

namespace ABMCEditAndReports.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Index(string returnUrl = null)
        {
            this.ViewBag.ReturnUrl = returnUrl;
            return View("LoginPage");
        }

        public ActionResult Index(LogInViewModel model, string returnUrl)
        {
            if (this.ModelState.IsValid && Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, false);
                return this.Redirect(returnUrl);
            }

            this.ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return this.View(model);
        }
    }
}

:

namespace ABMCEditAndReports.Models
{
    public class LogInViewModel
    {
        [Display(Name = "User Name")]
        public string UserName { get; set; }

        [Display(Name = "Password")]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    }
}

:

@model ABMCEditAndReports.Models.LogInViewModel
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
{
    @Html.ValidationSummary()
    <h2>Please sign in</h2>
    <div>
      @Html.DisplayNameFor(model => model.UserName)
      @Html.EditorFor(model => model.UserName)
    </div>
    <div>
      @Html.DisplayNameFor(model => model.Password)
      @Html.EditorFor(model => model.Password)
    </div>
    <button type="submit">Sign in</button>
}
+2

Source: https://habr.com/ru/post/1608304/


All Articles