在视图MVC中运行方法

时间:2017-05-23 10:36:46

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

我有这个登录页面,我必须完成,但我想实现第一次登录密码更改,我有一个方法在我的网络服务上检查它是否&#39 ; s第一次登录,另一次更改密码并将其保存在我的数据库中,事情是,我有登录页面有2个输入框,一个用于用户名,另一个用于密码,一旦我点击&# 39;登录'我想让它检查它是否是第一次,如果是,将2个当前输入框改为' Insert new pwd'和#重复新的pwd'但我不知道如何做到这一点。我认为它在视图中,但我似乎无法找到任何地方。 这是我的观点:

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

1 个答案:

答案 0 :(得分:0)

在您的LoginEscolas Action中检查您的数据库,看它是否是第一次登录。为此,如果用户已经登录,您应该在Users表中有一个字段进行保存。如果是第一次登录,您可以将用户重定向到另一个具有不同视图的操作,让我们说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
}