简体   繁体   English

每次用户被定向到视图/页面时,如何让我的 Javascript 不弹出

[英]How can I make my Javascript not pop up every time a user gets directed to the a View/Page

Currently I have a login page where if the user enters an invalid username or password and clicks "Login", a pop up message will display and say "Invalid username or password".目前我有一个登录页面,如果用户输入无效的用户名或密码并单击“登录”,则会显示一条弹出消息并显示“无效的用户名或密码”。 This is fine, but the issue I'm facing now is that when I'm coming back to the Login page after a User signs up, or if the client is being redirected to the Login page, the same pop up message will display saying "Invalid username or password".这很好,但我现在面临的问题是,当我在用户注册后返回登录页面时,或者如果客户端被重定向到登录页面,则会显示相同的弹出消息说“无效的用户名或密码”。

How can I make my code such that I avoid this issue?我怎样才能让我的代码避免这个问题? When a user signs up and goes back to the login page, the "Invalid username or password" alert should not appear.当用户注册并返回登录页面时,不应出现“无效的用户名或密码”警报。

Here are my codes: HTML/View这是我的代码:HTML/View

@{ 
    string loginError = (string) ViewData["loginError"];
}

@if (loginError != null)
{
<script type="text/javascript">
var message = '@loginError';
if(message)
    alert(message);
</script>
}


<form class="login-form" action="/Login/Login" method="POST">

    <div class="login-form__content">

        <div class="login-form__header">Login to your account</div>

        <input class="login-form__input" type="text" name="username" placeholder="Username">

        <input class="login-form__input" type="password" name="password" placeholder="Password">

        <button class="login-form__button" type="submit">Login</button>

        <p></p>

        <button class="login-form__button" type="submit" formaction="/Register/Index" id="signupbutton">Sign up</button>

    </div>

</form>

Controller:控制器:

public IActionResult Index()
        {
            if (Request.Cookies["SessionId"] != null)
            {
                string sessionId = Request.Cookies["sessionId"];
                Session session = dbContext.Sessions.FirstOrDefault(x =>
                    x.Id == sessionId)
                ;

                if (session == null)
                {
                    return RedirectToAction("Index", "Logout");
                }
                
                return RedirectToAction("Index", "Gallery");
            }

            string errorMessage = (string)TempData["loginError"];
            if (errorMessage != null)
            {
                ViewData["loginError"] = errorMessage;
            }

            return View();
        }

        public IActionResult Login(IFormCollection form)
        {
            string username = form["username"];
            string password = form["password"];

            HashAlgorithm sha = SHA256.Create();
            byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(username + password));

            User user = dbContext.Users.FirstOrDefault(x =>
               x.Username == username && x.PassHash == hash);

            if (user == null)
            {
                TempData["loginError"] = "Invalid username or password";
                return RedirectToAction("Index", "Login");
            }

            Session session = new Session()
            {
                User = user
            };
            dbContext.Sessions.Add(session);
            dbContext.SaveChanges();

            Response.Cookies.Append("SessionId", session.Id.ToString());
            Response.Cookies.Append("Username", user.Username);

            return RedirectToAction("Index", "Gallery");
        }

Change the following block:更改以下块:

if (user == null)
{
     TempData["loginError"] = "Invalid username or password";
     return RedirectToAction("Index", "Login");
}

With this:有了这个:

if (user == null &&
    (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password)))
    {
        TempData["loginError"] = "Invalid username or password";
        return RedirectToAction("Index", "Login");
    }

The above code will ignore login requests which are completely empty.上面的代码将忽略完全为空的登录请求。 You should not even validating if the user name field is not inputted.如果未输入用户名字段,您甚至不应该进行验证。 To validate an user account, the user must input one of the fields (username or password).要验证用户帐户,用户必须输入字段之一(用户名或密码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每当用户点击页面底部时,如何迭代此函数以进行ajax调用? - How can I iterate this function to make the ajax call every time the user hits the bottom of the page? 我怎样才能让这个自动弹出? - How can I make this pop up automatic? 我该怎么做 <p> 标签可点击通过使用PHP弹出? - How can i make every <p> tag clickable on a pop-up by using php? 每次我的 JavaScript 文件从其中提取代码时,如何使变量“刷新”自身? - How can I make a variable “refresh” itself every time my JavaScript file has code pulled from it? 如何制作弹出式窗口,以创建其他弹出式窗口? - How can I make a pop up that creates other pop ups? 如何让我的密码提示只在我打开我的网站时出现一次,而不是每次我重新加载页面或重新访问它时出现 - How do I make my password prompt only show up once when I open up my website instead of every time I reload the page or revisit it 每次刷新页面时如何加载用户? - How can I load user every time the page is refreshed? 如何在javascript中调用HTML页面并使之成为弹出窗口? - How to call a html page and make it as pop up window in JavaScript? 如何在用户点击链接后显示弹出窗口? - How can I make a pop up stop appearing after a user clicks the link? 如何使用javascript制作带有复选框的弹出窗口? - How can i make a pop-up window with checkboxes using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM