0

I have created a database in Microsoft sql server express. I need to be able to login on Mvc 2 app, using my database ( not the one existing on AcountController meaning MembershipService )

I just need to replace MemeberAhipService with my database. How can I do that ( i'm using entity framework code first ) . I don't need to create a model in visual studio. I have the usermodel, userContext: Db . I think i need repository also. Can anyone show me an example, of tell me the steps ?

1 Answer 1

1

You can create your own MembershipService.

Example:

New MembershipService.cs (or whatever you want)

public class MembershipService
{
    public bool IsUserValid(string username, string password)
    {
         var db = new DatabaseContext();
         var user = db.GetUser(username, password);
         // Or however you want to get your data, via Context or Repository
         return (user != null); 
    }
}

New FormsClass.cs

public class FormService
{
     public void SignIn(string username, List<string> roles)
     {

            FormsAuthenticationTicket authTicket = new
                            FormsAuthenticationTicket(1,            // Version
                            username,                               // Username
                            DateTime.Now,                           // Creation
                            DateTime.Now.AddMinutes(30),            // Expiration
                            false,                                  // Persistent
                            string.Join(",", roles.ToArray()));     // Roles

            string encTicket = FormsAuthentication.Encrypt(authTicket);
            HttpContext.Current.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));

            GenericIdentity id = new GenericIdentity(username);
            HttpContext.Current.User = new GenericPrincipal(id, roles.ToArray());
     }
}

In Global.asax:

        protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
        {
            HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {
                string encTicket = authCookie.Value;
                if (!String.IsNullOrEmpty(encTicket))
                {
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(encTicket);
                    FormsIdentity id = (FormsIdentity)Context.User.Identity;
                    var roles = ticket.UserData.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                    GenericPrincipal prin = new GenericPrincipal(id, roles);
                    HttpContext.Current.User = prin;

                }
            }

        }

Sign up to request clarification or add additional context in comments.

6 Comments

thx a lot. I will try doing that. I am new in mvc also entity framework. Do u have some links where i can read more about repository or doing something like login from database?
Then public IMembershipService MembershipService { get; set; } will become just: public MembershipService { get; set; }
Then public IMembershipService MembershipService { get; set; } will become just: public MembershipService { get; set; } . In this class we will have the properties ** username** , ** password**?
You can just create an interface for MembershipService to inherit from, you can have a property called Username, I'd advise against using a Password as a property
Sorry for keep asking. Just one more question, please. The DatabaseContext from yout example should look like this: public DbSet<User> LogonUser { get; set; } where in Class User I have the 2 properties : **username, password . The property GetUser with what should be populated? I would be really gratefull if u may help
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.