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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:46:41+00:00 2026-06-17T06:46:41+00:00

I want to use my custom User table in MVC 4 code first application.

  • 0

I want to use my custom User table in MVC 4 code first application. I defined advance User table in my database context:

public class MyDatabase : DbContext
    {
       public DbSet<User> UserSet { get; set; }
       public DbSet<News> NewsSet { get; set; }
       ...
    }

Model is like:

 public class User 
    {
        [Key]
        public int Id{ get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Name { get; set; }
        public string SurName { get; set; }
        ...
    }

When application start, it calls this:

WebSecurity.InitializeDatabaseConnection("MyDatabase", "Users", "Id", "UserName", autoCreateTables: true);

In controller I use Add(entity) to save user entity. After saving I want to log in user. But it does not work:

    [HttpPost]
    public ActionResult Register(User user)
    {
     var result =  MyService.SaveUser(user);
     WebSecurity.Login(result.UserName, result.Password, true);
     return RedirectToAction("Index", "Profile", new { id = result.Id });
    }

After saving user, it’s data stored in my database, but it can not log in. How should I do?

Edit:

Is it right to save User entity with my business method? Or I must do it only with
WebSecurity.CreateUserAndAccount()?

If I can use my own save method, how to save password in database?

  • 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-06-17T06:46:42+00:00Added an answer on June 17, 2026 at 6:46 am

    You could just use forms authentication directly.

     [HttpPost]
        public ActionResult Register(User user)
        {
         var result =  MyService.SaveUser(user);
         SignIn(result.Id, "");
         return RedirectToAction("Index", "Profile", new { id = result.Id });
        }
    
    
    public void SignIn(string accountId, string roles)
                {
                    var authTicket = new FormsAuthenticationTicket(
                        1,
                        accountId,
                        DateTime.Now,
                        DateTime.Now.AddMinutes(20),
                        false,
                        roles
                        );
    
                    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                    var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
    
                    HttpContext.Current.Response.Cookies.Add(authCookie);
                }
    

    Here is a user class that will help you with password issue. It relies on BCrypt

     public class UserAccount
        {
            public string Id { get; set; }
            public string Username { get; set; }
            public string Email { get; set; }
    
            public string Password
            {
                get { return PasswordHash; }
                set { PasswordHash = HashPassword(value); }
            }
    
            public string PasswordHash { get; private set; }
    
            public List<string> Roles { get; set; }
    
            public string AuthenticationRoles
            {
                get { return Roles == null ? "" : String.Join(",", Roles.Select(x => x.ToString())); }
            }
    
            public bool IsActive { get; set; }
    
            public string Name { get; set; }
    
            public bool PasswordIsValid(string password)
            {
                bool matches = BCrypt.Net.BCrypt.Verify(password, Password);
                return matches;
            }
    
            private string HashPassword(string value)
            {
                return BCrypt.Net.BCrypt.HashPassword(value);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use Custom URL Scheme to transfer data from application to other
I want to use a custom class that could handle querying easily. Are there
I have developed MVC web app. Now, I want to use custom form authentication
I want to use custom exception handling, for example instead of using (Exception ex)
For example I want to use custom logger: logger = require('basic-logger'), logger.setLevel('info') var customConfig
I want to use a custom template file which shoud use the base layout
I want to use a custom property to interact between controllers. Or do I
I am having an issue in iphone when i want to use a custom
I want to use the Google Custom Search API (or custom search engine directly)
Just want to ask if i can use Custom Validator in client side without

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.