开发者

mvc entity framework : login using a database

开发者 https://www.devze.com 2023-04-03 23:03 出处:网络
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 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 ?


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;

                }
            }

        }

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号