Asp.net mvc 2 mvc实体框架:使用数据库登录

Asp.net mvc 2 mvc实体框架:使用数据库登录,asp.net-mvc-2,login,entity-framework-4.1,login-control,loginview,Asp.net Mvc 2,Login,Entity Framework 4.1,Login Control,Loginview,我已在Microsoft sql server express中创建了一个数据库。我需要能够登录Mvc 2应用程序,使用我的数据库(不是现有的一个在帐户控制器意味着会员服务) 我只需要用我的数据库替换MemeberAhipService。我如何做到这一点(我首先使用的是实体框架代码)。我不需要在VisualStudio中创建模型。我有usermodel,userContext:Db。我想我也需要存储库。谁能给我举个例子,告诉我步骤 您可以创建自己的会员服务 例如: New MembershipS

我已在Microsoft sql server express中创建了一个数据库。我需要能够登录Mvc 2应用程序,使用我的数据库(不是现有的一个在帐户控制器意味着会员服务)


我只需要用我的数据库替换MemeberAhipService。我如何做到这一点(我首先使用的是实体框架代码)。我不需要在VisualStudio中创建模型。我有usermodel,userContext:Db。我想我也需要存储库。谁能给我举个例子,告诉我步骤

您可以创建自己的会员服务

例如:

New MembershipService.cs(或您想要的任何内容)

公共类会员服务 { public bool IsUserValid(字符串用户名、字符串密码) { var db=新数据库上下文(); var user=db.GetUser(用户名、密码); //或者通过上下文或存储库获取数据的方式 返回(用户!=null); } } 新表格class.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()); } } 公共类表格服务 { 公共无效登录(字符串用户名、列表和ltstring>角色) { FormsAuthenticationTicket authTicket=新建 FormsAuthenticationTicket(1,//版本 用户名,//用户名 DateTime.Now,//创建 DateTime.Now.AddMinutes(30),//过期 false,//持久 string.Join(“,”,roles.ToArray());//roles 字符串encTicket=FormsAuthentication.Encrypt(authTicket); HttpContext.Current.Response.Cookies.Add(新的HttpCookie(FormsAuthentication.formscookeName,encTicket)); GenericEntity id=新的GenericEntity(用户名); HttpContext.Current.User=新的GenericPrincipal(id,roles.ToArray()); } } 在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; } } } 受保护的无效应用程序\u PostAuthenticateRequest(对象发送方,事件参数e) { HttpCookie authCookie=HttpContext.Current.Request.Cookie[FormsAuthentication.FormScookeName]; if(authCookie!=null) { 字符串encTicket=authCookie.Value; 如果(!String.IsNullOrEmpty(encTicket)) { FormsAuthenticationTicket票证=FormsAuthentication.Decrypt(encTicket); FormsIdentity id=(FormsIdentity)Context.User.Identity; var roles=ticket.UserData.Split(“,”.ToCharArray(),StringSplitOptions.RemoveEmptyEntries); GenericPrincipal prin=新的GenericPrincipal(id、角色); HttpContext.Current.User=prin; } } }
非常感谢。我会试试的。我也是mvc实体框架的新手。你有一些链接可以让我阅读更多关于存储库的信息或者做一些类似于从数据库登录的事情吗?然后public-IMembershipService-MembershipService{get;set;}将变成just:public-MembershipService{get;set;}然后public-IMembershipService-MembershipService{get;set;}将变成just:public-MembershipService{get;set;}。在这个类中,我们将有属性**用户名**,**密码**?您可以创建一个接口,让MembershipService从中继承,您可以有一个名为username的属性,我建议不要使用密码作为属性。请继续提问。请再问一个问题。您示例中的DatabaseContext应该如下所示:public DbSet LogonUser{get;set;},其中在类中User I有两个属性:**用户名,密码。属性GetUser应该填充什么?如果你能帮忙,我将不胜感激 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; } } }