Login form


Register
public class LoginModel
{
    [Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(Resource))]
    [Display(Name = "Email", Prompt = "Email", ResourceType = typeof(Resource))]
    [BsControl(BsControlType.Email)]
    public string Email { get; set; }

    [Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(Resource))]
    [Display(Name = "Password", Prompt = "Password", ResourceType = typeof(Resource))]
    [BsControl(BsControlType.Password)]
    public string Password { get; set; }

    [Display(Name = "RememberMe", ResourceType = typeof(Resource))]
    [BsControl(BsControlType.CheckBox)]
    public bool RememberMe { get; set; }
}
            
@using (Html.BsBeginForm(
	"Index", 
	"Login",
	new {ReturnUrl = ViewBag.ReturnUrl}, 
	FormMethod.Post, 
	new {@class = "js-loginForm"}))
{
    <div class="col-lg-12">
        @Html.BsValidationSummary()
    </div>

    <div class="col-lg-12 form-group @Html.BsValidationCssFor(m => m.Email)">
        <div class="input-group">
            @Html.BsGlyphiconAddon(Glyphicon.Envelope)
            @Html.BsInputFor(m => m.Email)
            @Html.BsValidationFor(m => m.Email)
        </div>
    </div>

    <div class="col-lg-12 form-group @Html.BsValidationCssFor(m => m.Password)">
        <div class="input-group">
            @Html.BsGlyphiconAddon(Glyphicon.Lock)
            @Html.BsInputFor(m => m.Password)
            @Html.BsValidationFor(m => m.Password)
        </div>
    </div>

    <div class="col-lg-12 form-group checkbox">
        @Html.BsInputFor(m => m.RememberMe)
    </div>

    <div class="col-lg-12">
        <hr />
        <button class="btn btn-default js-loginBtn" type="submit">Login</button>
        <a class="btn btn-primary pull-right" href="@Url.Action("Index", new {mode = "register"})">Register</a>
    </div>
}
            
public class LoginController : BaseController
{
    [HttpGet]
    public ActionResult Index(string mode)
    {
        var model = new AuthenticationModel()
        {
            LoginModel = new LoginModel(),
            RegisterModel = InitRegisterModel()
        };

        return View(model);
    }

    [HttpPost]
    public ActionResult Index(AuthenticationModel model, string mode)
    {
	//validate credentials and redirect

	//if authorization failed add global validation error
	ModelState.AddFormError("LoginModel",
		"This account has been suspended! Contact us for more details.");

	//return view with validation errors
	return View(model);
    }
}
            
require([
	'bforms-initUI',
	'bforms-validate-unobtrusive'
], function () {

    var LoginIndex = function (options) {
        this.options = $.extend(true, {}, options);
    };

    LoginIndex.prototype.init = function () {

        this.$loginForm = $('.js-loginForm');

        //apply BForms plugins
        this.$loginForm.bsInitUI(this.options.styleInputs);
    };

    $(document).ready(function () {
        var ctrl = new LoginIndex(requireConfig.pageOptions);
        ctrl.init();
    });
});
            
Loading...