如何将登录表单移至导航栏

时间:2019-04-16 05:56:52

标签: c# asp.net .net model-view-controller

我目前正在解决一个我一直在尝试通过“边做边学”解决的问题,但是我无处可去,几乎要独自解决它,让它按照我知道的方式运行。

现在的工作方式: 目前,我已经从asp.net整理了整个身份区域,登录和注册都在单独的视图中进行

我想要的方式: 登录名应放在导航栏中,但要这样做,我需要模型粘贴用户名和密码。如果我在_LoginPartial中使用模型,则注册不起作用。目前,我可以将登录表单正常移动到导航栏并使用该表单正常登录/注销,但是随后我不再被允许注册,因为它想要我的注册页面的登录模型。

如果需要,我可以添加其他代码,但是它们或多或少是默认的围巾类。

Picture Illustration

_LoginPartial

@inject SignInManager<User> SignInManager
@inject UserManager<User> UserManager
@using Microsoft.AspNetCore.Identity
@using TVScreenViewer.Models.Identity
@model TVScreenViewer.Areas.Identity.Pages.Account.LoginModel

@if (SignInManager.IsSignedIn(User))
{
    <form asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new {area = ""})" method="post" id="logoutForm" class="navbar-nav navbar-right">
        <ul class="navbar-nav navbar-right">
            <li class="nav-item">
                <a class="nav-link" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @UserManager.GetUserAsync(User).Result.Name!</a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown1" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Menu
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown1">
                    <a class="dropdown-item" href="#">Settings</a>
                    <div class="dropdown-divider"></div>
                    <button type="submit" class="btn btn-secondary btn-block" style="padding: 4px 24px; text-align: left;">Logout</button>
                </div>
            </li>
        </ul>
    </form>
}
else
{
    <form asp-area="Identity" asp-page="/Account/Login" method="post" class="navbar-nav navbar-right">
        <div asp-validation-summary="All" class="text-danger"></div>
        <div class="row">
            <div style="margin: 0rem 1rem">
                <input class="form-control form-control-sm" asp-for="Input.Username"/>
                <span asp-validation-for="Input.Username" class="text-danger"></span>
            </div>
            <div>
                <input class="form-control form-control-sm" asp-for="Input.Password"/>
                <span asp-validation-for="Input.Password" class="text-danger"></span>
            </div>
            <div style="margin: 0rem 1rem">
                <button type="submit" class="btn btn-primary btn-sm">Log in</button>
            </div>
        </div>
    </form>

    @*<a asp-area="Identity" asp-page="/Account/Login">Login</a>*@
    <a asp-area="Identity" asp-page="/Account/Register">Register</a>
}

@section Scripts {
    <partial name="_ValidationScriptsPartial"/>
}

1 个答案:

答案 0 :(得分:-1)

查找用户界面:

以下是引导程序3中的示例,您可以根据需要使用登录表单: https://getbootstrap.com/docs/3.4/examples/jumbotron/

和第4版中的另一个是搜索栏: https://getbootstrap.com/docs/4.3/examples/sticky-footer-navbar/

您可以检查元素: enter image description here

并复制此部分(取决于版本,可能有所不同 enter image description here

编码(实际问题)

自行登录本身并不特殊 您可以使用旧的html表单或MVC beginForm并将操作设置为将数据发送到“帐户”页面...通过服务器执行身份验证,然后服务器分配cookie,因此只要将数据发送到服务器就没有关系。并且您需要根据型号输入两次,可以是用户名和密码,

还有这些:asp-pageasp-for ...对我来说是新手,我不记得在任何地方都看到过它们,请确保您使用的是@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })这种rezor支持的字段,或使用<input name="">属性的name html输入

这也是Microsoft在.NetBased MVC上的默认格式:

@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))

用于登录的默认Microsoft代码

注意 我注意到由于@using Microsoft.AspNetCore.Identity这行代码,您正在使用Core MVC,它的模板可能与.netframwork模板不同,但是概念应该相同

@using OpenAndDelete.Models
@model LoginViewModel
@{
    ViewBag.Title = "Log in";
}

<h2>@ViewBag.Title.</h2>
<div class="row">
    <div class="col-md-8">
        <section id="loginForm">
            @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                @Html.AntiForgeryToken()
                <h4>Use a local account to log in.</h4>
                <hr />
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                <div class="form-group">
                    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <div class="checkbox">
                            @Html.CheckBoxFor(m => m.RememberMe)
                            @Html.LabelFor(m => m.RememberMe)
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Log in" class="btn btn-default" />
                    </div>
                </div>
                <p>
                    @Html.ActionLink("Register as a new user", "Register")
                </p>
                @* Enable this once you have account confirmation enabled for password reset functionality
                    <p>
                        @Html.ActionLink("Forgot your password?", "ForgotPassword")
                    </p>*@
            }
        </section>
    </div>
    <div class="col-md-4">
        <section id="socialLoginForm">
            @Html.Partial("_ExternalLoginsListPartial", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })
        </section>
    </div>
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}