【问题标题】:Running methods inside the view MVC在视图 MVC 中运行方法
【发布时间】:2017-05-23 10:36:46
【问题描述】:

我有这个我必须做的登录页面,它已经完成了,但我想实现第一次登录密码更改,我的网络服务上有一个方法来检查它是否是第一次登录,还有另一个一个更改密码并将其保存在我的数据库中,问题是,我有一个带有 2 个输入框的登录页面,一个用于用户名,另一个用于密码,一旦我点击“登录”,我希望它检查它是否是第一次,如果是,将 2 个当前输入框更改为“插入新密码”和“重复新密码”,但我不知道如何做到这一点。我认为它在视图中,但我似乎无法找到如何在任何地方。 这是我的看法:

@model ProjectoEscolas.Models.UserGetInfo
@{
   ViewBag.Title = "LoginEscolas";
 }
<link href="@Url.Content("~/bower_components/font-awesome/css/font-awesome.min.css")" rel="stylesheet" type="text/css" />
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
<link href="~/Content/login.css" rel="stylesheet" />

@using (Html.BeginForm("LoginEscolas", "Home", FormMethod.Post))
{
<body>
    <img class="logotipos" src="../Images/logotipos.png"/>
    <div class="login-form">
        <!--<img class="loginimg" src="../Images/logotipos.png">-->
        <h1>Área Privada</h1>
        <p>Para entrar na Área Privada da DGEstE, deverá escrever o Utilizador e a Palavra-Chave das aplicações da <br /> DGEEC(Ex-GEPE)</p>
        <br />            
        <div class="form-group ">
            @Html.TextBoxFor(u => u.username, new { placeholder = "Utilizador", @class = "form-control" })
            <i class="fa fa-user"></i>
        </div>
        <div class="form-group log-status">
            @Html.PasswordFor(u => u.password, new { placeholder = "Palavra-Chave", @class = "form-control" })
            <i class="fa fa-lock"></i>
        </div>
        <span class="alert">Invalid Credentials</span>
        <input type="submit" value="Log In" class="log-btn">           
        @if (ViewBag.Error != null)
        { 
            <h4 style="color:red">@ViewBag.Error</h4>
        }
    </div>
</body>
}

这是我正在使用的控制器的一部分:

    [HttpPost]
    public ActionResult LoginEscolas(UserGetInfo user)
    {
        Debug.WriteLine("Error");
        if (user.username == null || user.password == null)
        {
            ViewBag.Error = "Insira os campos obrigatórios";
            return View(user);
        }
        else
        {
            if (isValid(Convert.ToInt32(user.username), user.password))
            {
                FormsAuthentication.SetAuthCookie(user.username, false);
                return RedirectToAction("Index", "Home");
            }
        }

        return View(new UserGetInfo());
    }

    //public ActionResult LogOut()
    //{
    //    FormsAuthentication.SignOut();
    //    return RedirectToAction("Login", "Home");
    //}

    private bool isValid(int username, string password)
    {
        bool isValid;

        int id = Methods.LoginEscolas(username, password);
        if (id != 0)
        {

            Methods.InsertLogs_Escolas(id);
            Debug.WriteLine(id);
            isValid = true;
        }
        else
        {
            isValid = false;
            Debug.WriteLine(id);
            ViewBag.Error = "Insira os seus dados correctamente";
        }
        return isValid;
    }

    public bool haschanged(int username,string password)
    {
        int id = Methods.LoginEscolas(username, password);
        if (!Methods.hasChangedPwd_Escolas(id))
        {
            return false;
        }else
        {
            return true;
        }
    }

    public void changePwd(int username,string password,string newPwd)
    {
        int id = Methods.LoginEscolas(username, password);
        Methods.changePwd_Escolas(id, newPwd);
    }

【问题讨论】:

  • 这些应该是 2 个单独的方法和视图(具有单独的视图模型) - 一个 Register 操作和一个 Login 操作
  • @StephenMuecke 不会有Register,这是针对公司的,公司用户会得到一个ID和密码,第一次登录时,他们必须更改密码.抱歉没有说清楚

标签: c# asp.net-mvc login login-control


【解决方案1】:

在你的 LoginEscolas Action 检查你的数据库,看看它是否是第一次登录。为此,如果用户已经登录,您应该在用户表中保存一个字段。如果是第一次登录,您可以将用户重定向到具有不同视图的另一个操作,比如说ChangePassword。现在在ChangePassword 视图中,您可以从用户那里获取新密码。

[HttpPost]
public ActionResult LoginEscolas(UserGetInfo user)
{
    Debug.WriteLine("Error");
    if (user.username == null || user.password == null)
    {
        ViewBag.Error = "Insira os campos obrigatórios";
        return View(user);
    }
    else
    {
        if (isValid(Convert.ToInt32(user.username), user.password))
        {
            FormsAuthentication.SetAuthCookie(user.username, false);
            return RedirectToAction("Index", "Home");
        }
    }
    if(isFirstLogin(Convert.ToInt32(user.username), user.password)))
    {
        setFirstLogin(Convert.ToInt32(user.username));
        return RedirectToAction("ChangePassword");
    }
    return View(new UserGetInfo());
}
[HttpGet]
public ActionResult ChangePassword(int userName)
{
    var model = new UserGetInfo {
        username = username
    };
    return View(model);
}
[HttpPost]
public ActionResult ChangePassword(int userName, string newPassword)
{
    // Change user password
}
public bool isFirstLogin(int username)
{
    // Check database to see if it is first login
}
public bool setFirstLogin(int username)
{
    // Set FirstLogin to false for this user
}

【讨论】:

    猜你喜欢
    • 2018-06-08
    • 2011-10-16
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    相关资源
    最近更新 更多