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 7672901
In Process

The Archive Base Latest Questions

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

I am having two issues: I usually create separate view for the login to

  • 0

I am having two issues:

  1. I usually create separate view for the login to keep things simple but this time i want to have in the master page. If something goes wrong with the login process i add some error in the modelstate and keep the user to the page he currently is. What is the proper way to construct this, i mean create the current url?

  2. In the register view, after submit, if that username exists, i see that model validation error in both validation summary(login form validation summary and register form validation summary).What can i do see the error message only in one particular form validation summary?

All the code is in HomeController but i will rename it to AccountController and keep here only ViewResult Index().

I gladly accept any tips of how to write it better and of course any critics.
Thanks.

    public class HomeController : Controller
    {
        private List<string> questions = new List<string>
        {
            "What is yout pet's name?",
            "What is your birth date?(mm/dd/yyyy)"
        };

        private CustomMembershipProvider mbProvider = (CustomMembershipProvider)System.Web.Security.Membership.Provider;

        public ViewResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginModel logModel, string returnUrl)
        {
            if (String.IsNullOrEmpty(logModel.LoginUsername) || String.IsNullOrEmpty(logModel.LoginPassword))
            {
                ModelState.AddModelError(string.Empty, "Please enter username and password.");
                return Current_View;
            }
            else
            {
                MembershipUser mbUser = mbProvider.GetUser(logModel.LoginUsername);
                if (mbUser == null)
                {
                    ModelState.AddModelError(string.Empty, "Invalid username.");
                    return Current_View;
                }
                else
                {
                    if (!mbUser.IsApproved)
                    {
                        ModelState.AddModelError(string.Empty, "Go to your email and confirm the registration.");
                        return Current_View;
                    }
                    if (mbUser.IsLockedOut)
                    {
                        ModelState.AddModelError(string.Empty, "Your account is locked.");
                        return Current_View;
                    }
                    bool isValid = mbProvider.ValidateUser(logModel.LoginUsername, logModel.LoginPassword);
                    if (!isValid)
                    {
                        ModelState.AddModelError(string.Empty, "Invalid credetials.");
                        return Current_View;
                    }
                    else
                    {
                        FormsAuthentication.SetAuthCookie(logModel.LoginUsername, false);
                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return Current_View;
                        }
                    }
                }
            }            
        }

        private void SetRegisterViewData()
        {
            ViewData["Questions"] = new SelectList(questions);
        }

        public ViewResult Register()
        {
            SetRegisterViewData();
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegistrationModel regInfo)
        {
            SetRegisterViewData();
            if (ModelState.IsValid)
            {
                MembershipCreateStatus status;
                MembershipUser user = mbProvider.CreateUser(regInfo.UserName, regInfo.PassWord, regInfo.Email, regInfo.SecretQuestion, regInfo.SecretAnswer, false, null, out status);
                switch (status)
                {
                    case MembershipCreateStatus.DuplicateEmail:
                        ModelState.AddModelError("Email", "That email already exists");
                        return View(regInfo);
                    case MembershipCreateStatus.DuplicateUserName:
                        ModelState.AddModelError("Username", "That username already exists");
                        return View(regInfo);
                    case MembershipCreateStatus.Success:
                        //SendConfirmationMail(regInfo.UserName);
                        return RedirectToAction("Index");
                    default:
                        ModelState.AddModelError("", "Error");
                        return View(regInfo);
                }
            }            
            return RedirectToAction("Index");
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index");
        }
    }
  • 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-31T16:23:27+00:00Added an answer on May 31, 2026 at 4:23 pm

    For your validation I would suggest using DataAnnotations.

    In LoginModel.cs you can define your criteria and error messages.

    using System.ComponentModel.DataAnnotations;
    
    public class LoginModel{
        [Required(AllowEmptyStrings = false, ErrorMessage = @"You must supply a valid email address.")]
        [DataType(DataType.EmailAddress)]
        public String Email { get; set; }
    
        [Required(AllowEmptyStrings = false)]
        [DataType(DataType.EmailAddress)]
        [Compare("Email", ErrorMessage = @"Passwords do not match")]
        public String ConfirmEmail { get; set; }
    
        [Required(AllowEmptyStrings = false, ErrorMessage = @"Please enter a password")]
        [DataType(DataType.Password)]
        public String Password { get; set; }
    
        //whatever other properties you want
    }
    

    Then in your controller you can just check for:

    if(ModelState.IsValid()){
        //do stuff
    }
    

    If not then return View(logModel) and the errors will be present in that object.

    Appended

    [HttpPost]
    public ActionResult Login(LogInModel loginModel){
        if(ModelState.IsValid()){
            //do stuff if good
        }
        //not valid
        TempData["loginModel"] = loginModel;
        RedirectToAction("Index","Home");
    }
    

    In the Home ActionResult just add logic to handle the loginModel object which will have the errors in it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having some issues getting this MPMoviePlayerViewController to work. I have two sample URLs pointing
We are having issues with our SQL Server. About two times a day we
How do I go about having two view models communicate with one another using
I am currently stuck, having two separate glossaries: main & acronyms . Acronyms glossary
i have two sheets having data like this sheet1 : **A** **B** **C** 752
I am having issues passing two coordinates from one function to another. I don't
So I'm having two issues that I cannot seem to get unkinked. I run
I seem to be having two issues with my project after I converted from
Well I am having two issues that i can't get to work, related to
I am having two issues with my code - I am not sure how

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.