Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6867755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:20:57+00:00 2026-05-27T03:20:57+00:00

[HttpPost] public ActionResult Login(LoginModel model) { if (ModelState.IsValid) { Authenticate(model.Username, model.Password); return RedirectToAction(Index); }

  • 0
[HttpPost]
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                Authenticate(model.Username, model.Password);
                return RedirectToAction("Index");
            }
            else
            {
                return View(model);
            }
        }

        private void Authenticate(string userName, string password)
        {
            const string commaSeperatedRoles = "Administrator,Editor";

            if (userName == "xx" && password == "xxx")
            {
                FormsAuthenticationUtil.RedirectFromLoginPage(userName, commaSeperatedRoles, false);
            }
        }

and LoginModel

public class LoginModel
    {
        [Required(ErrorMessage="*")]
        [StringLength(10)]
        public string Username { get; set; }

        [Required(ErrorMessage = "*")]
        [StringLength(10)]
        public string Password { get; set; }
    }

and view

@using (Html.BeginForm())
{
    <table width="100%">
    <tr>
        <td>Username:</td>
        <td>
            @Html.TextBoxFor(m => m.Username, new { @style = "width: 140px;" })
        </td>
    </tr>
    <tr>
        <td>Passwd:</td>
        <td>
            @Html.PasswordFor(m => m.Password, new { @style = "width: 140px;" })
        </td>
    </tr>
    </table>
    <div class="napaka">Wrong username and password</div>
    <br />
    <input type="submit" class="button" value="Login" />
}

but now i always get this message of wrong login “Wrong username and password”. How can i write this message only when username and password are wrong? I am using mvc3 in C#. Can i somehow send bool variable to view or what is the sbet way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:20:58+00:00Added an answer on May 27, 2026 at 3:20 am

    In case of wrong credentials add an error message to the model state:

    [HttpPost]
    public ActionResult Login(LoginModel model)
    {
        if (ModelState.IsValid)
        {
            if (Authenticate(model.Username, model.Password))
            {
                // authentication was successful. The method created the cookie
                // so we can safely redirect to an authenticated page here
                return RedirectToAction("Index");
            }
            else
            {
                // authentication failed due to wrong username or password =>
                // we add an error message to the ModelState here so that we 
                // can show it in the view
                ModelState.AddModelError("", "Wrong username and password");
            }
        }
    
        // At this stage we know that some error happened =>
        // we redisplay the view so that the user can fix it
        return View(model);
    }
    
    private bool Authenticate(string userName, string password)
    {
        const string commaSeperatedRoles = "Administrator,Editor";
        if (userName == "xx" && password == "xxx")
        {
            // Warning: you should not be redirecting here. You should only
            // create and set the authentication cookie. The redirect should be done
            // in an MVCish way, i.e. by returning a RedirectToAction result if this
            // method returns true
            FormsAuthenticationUtil.SetAuthCookie(userName, commaSeperatedRoles, false);
            return true;
        }
    
        // wrong username or password
        return false;
    }
    

    and in the view instead of hardcoding the error message in a div use the ValidationSummary helper:

    @using (Html.BeginForm())
    {
        <table width="100%">
        <tr>
            <td>Username:</td>
            <td>
                @Html.TextBoxFor(m => m.Username, new { @style = "width: 140px;" })
            </td>
        </tr>
        <tr>
            <td>Passwd:</td>
            <td>
                @Html.PasswordFor(m => m.Password, new { @style = "width: 140px;" })
            </td>
        </tr>
        </table>
    
        @Html.ValidationSummary(false)
        <br />
        <input type="submit" class="button" value="Login" />
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have 2 actions public ActionResult FilesAdd(int id) { FillParentMenuDDL(id); return View(); } [HttpPost]
here is my root controller : [HttpPost] public ActionResult Index() { } but it
Controller public ActionResult Create() { return View(new PageViewModel()); } [HttpPost] public ActionResult Create(Page page)
I have a Partial view (Login, with username, password and Submit button), and the
Take this snippet out of a controller, for example: public ActionResult Login() { if
In my MVC 3 application I have a very basic controller [HttpPost] public ActionResult
my controller action: [HttpPost] public ActionResult AddPointAndCopyOtherSongToPlaylist(int id) { if (CheckIfAddPointToSelf(User.Identity.Name, id)) { var
when updating ViewModels via [HttpPost] public ActionResult Edit(int id, AddressChooserEnum addressChooser, string btnSubmit, FormCollection
The controller is: public class HomeController : Controller { public ActionResult LogOn() { return
I have this url in my login page: http://localhost:5550/login?ReturnUrl=/forum/456 I have this controller: [HttpPost]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.