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

  • Home
  • SEARCH
  • 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 6126187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:20:28+00:00 2026-05-23T16:20:28+00:00

I’m trying to implement a simple login for admin in a MVC3 project. I’m

  • 0

I’m trying to implement a simple login for admin in a MVC3 project. I’m new to ASP.NET and to MVC3. I have googled and asked questions! have seen a lot of really nice ways of implementing this but they were all on a very abstract level and quite frankly maybe a bit to high for me at the time being. I have the credentials in my db so basically I just want to query that one and redirect the user if login matches those and if not show login form again. So this is what I got. My model:

  public class FormModel
{
    public bool isAdmin { get; set; }


 [Required(ErrorMessage = "Please enter your Username")]
 //[Remote("ValidateUserName", "Login", ErrorMessage = "UserName already taken!")]
 [StringLength(6, MinimumLength = 3)]
 [Display(Name = "Username:")]
 [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")] 
 public string UserName { get; set; }

 [Required(ErrorMessage = "Please enter your Password")]
 [DataType(DataType.Password)]
 [Display(Name = "Password:")]
 public string Password { get; set; }

}
 public User IsAdmin(string username, string password) 
   { 
      return (from user in db.Users 
      where user.username == username && user.password == password <--- alternative here is to just match username and pass against the data I have in my db columns(testdata 'admin', 'password')
      && user.IsAdmin == true  
      select user).SingleOrDefault(); 
    }

And in my controller basically this right now:

   public ActionResult Index()
    {
        //some code here maybe a conditional
        return View();
    }

And finally my view:

     @model Web.VoucherCannon.Models.FormModel
     @using (Html.BeginForm("HandleForm", "Login", FormMethod.Post, new {id = "myForm"})) {
@Html.ValidationSummary(true)
        <div class="editor-label">
        @Html.LabelFor(model => model.UserName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.UserName)
        @Html.ValidationMessageFor(model => model.UserName)
    </div> 
    <div class="editor-label">
        @Html.LabelFor(model => model.Password)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Password)
        @Html.ValidationMessageFor(model => model.Password)
    </div>
    <button class="button">Log In</button> 
      }

So now. How do I use the returned result of the query in my controller so that I can login? I’m sure I will refactor this later on and make it with a dbcontext layer and so on, but just for now I will be happy to make this work. Grateful for help!

  • 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-23T16:20:28+00:00Added an answer on May 23, 2026 at 4:20 pm

    You should have 2 controller actions: one for rendering the login (accessible on GET) form and one for handling the submission (accessible on POST) and performing the actual authentication.

    // This will render the Login view (the one you have shown)
    public ActionResult Login()
    {
        var model = new FormModel();
        return View(model);
    }
    
    // This one is responsible for handling the submission and credential verification
    [HttpPost]
    public ActionResult Login(FormModel model)
    {
        if (!ModelState.IsValid)
        {
            // The user submit the form but validation 
            // (as defined on the model using DataAnnotation attributes) failed
            // => redisplay the view so that the user can fix his errors
            return View(model);
        }
    
        // notice that you don't need to pass parameters to the IsAdmin method
        // as it already contains the username and password as properties
        if (!model.IsAdmin()) 
        {
            // The IsAdmin method didn't verify the credentials => add a model error
            // and redisplay the login view
            ModelState.AddModelError("username", "dude you are not an admin");
            return View(model);
        }
    
        // OK, at this stage everything is fine => we can grant access
        // by issuing an authentication cookie
        FormsAuthentication.SetAuthCookie(model.UserName, false);
    
        // finally we redirect to some home page for admins
        return RedirectToAction("Index", "Admin");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into

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.