首页 新闻 会员 周边

MVC 用户资料的问题

0
悬赏园豆:20 [已解决问题] 解决于 2013-05-06 22:19

刚刚接触mvc 在建立的项目中我看到mvc已经内置了用户的注册和登录功能,内置的AccountController 中的 Register 注册方法里面是使用 Membership.CreateUser 方法进行注册新用户的,参数为:

View Code
   //
        // 摘要:
        //     将具有指定的属性值和唯一的标识符的新用户添加到数据存储区,并返回一个状态参数,指示该用户是否成功创建或用户创建失败的原因。
        //
        // 参数:
        //   username:
        //     新用户的用户名。
        //
        //   password:
        //     新用户的密码。
        //
        //   email:
        //     新用户的电子邮件地址。
        //
        //   passwordQuestion:
        //     成员资格用户的密码提示问题的值。
        //
        //   passwordAnswer:
        //     成员资格用户的密码提示问题答案的值。
        //
        //   isApproved:
        //     一个布尔值,该值指示是否批准新用户登录。
        //
        //   providerUserKey:
        //     该用户的用户标识符,应存储在成员资格数据存储区。
        //
        //   status:
        //     一个 System.Web.Security.MembershipCreateStatus 值,该值指示该用户是否成功创建或用户创建失败的原因。
        //
        // 返回结果:
        //     用于新创建用户的 System.Web.Security.MembershipUser 对象。如果没有创建用户,此方法将返回 null。
        public static MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status);

我想问的是,这些都是写好的参数,如果我的项目中在新建用户的时候还需要填写一些其他信息,比如爱好,年龄,所在地等……那么我该如何去做?还有这个注册用户的方法注册的用户在哪个库里面,该怎么找这个库?如果想要添加注册项的话是不是还要修改那个数据库中的表结构?

请指教!感谢~

然后、没所以的主页 然后、没所以 | 初学一级 | 园豆:5
提问于:2013-05-03 16:20
< >
分享
最佳答案
0

个人觉得内置的这个注册和登录很不好用,有很大局限。最好是自己做,比较灵活。我建项目的时候都是选的“基本”,再自己加,也很快的

收获园豆:20
Meen | 菜鸟二级 |园豆:321 | 2013-05-04 11:48

那么如果自己做的话,有些action方法怎么才能知道用户是否已经登陆了呢? 用户登陆后的信息票据是怎么实现的?

然后、没所以 | 园豆:5 (初学一级) | 2013-05-04 13:41

@然后、没所以: 

View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Data;
  4 using System.Data.Entity;
  5 using System.Linq;
  6 using System.Web;
  7 using System.Web.Mvc;
  8 using Mvc_WebOffice.Models;
  9 using System.Web.Security;
 10 
 11 namespace Mvc_WebOffice.Controllers
 12 {
 13     public class AccountController : Controller
 14     {
 15         private AccountContext db = new AccountContext();
 16         private DAL.Account account = new DAL.Account();
 17 
 18         //
 19         // GET: /Account/
 20         [Authorize(Roles = "employee")]
 21         public ActionResult Index()
 22         {
 23             return View(account.GetUserList());
 24         }
 25 
 26         //
 27         //GET: /Account/Login
 28         public ActionResult Login()
 29         {
 30             ViewBag.roles = account.GetRoles(4);
 31             return View();
 32         }
 33 
 34         /// <summary>
 35         /// POST:登录
 36         /// </summary>
 37         /// <param name="inputUser">输入的用户信息</param>
 38         /// <returns></returns>
 39         [HttpPost]
 40         public ActionResult Login(User inputUser)
 41         {
 42             User user = account.GetUser(inputUser.UserName,inputUser.Password);
 43                  
 44             if (ModelState.IsValid)
 45             { 
 46                 if (user != null && user.Password == inputUser.Password)//如果通过验证
 47                 {
 48                     FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
 49                         1,
 50                         user.UserName,
 51                         DateTime.Now,
 52                         DateTime.Now.Add(FormsAuthentication.Timeout),
 53                         false,
 54                         account.GetRoles(user.ID)//获取权限(职位)
 55                         );
 56                     HttpCookie cookie = new HttpCookie(
 57                     FormsAuthentication.FormsCookieName,
 58                     FormsAuthentication.Encrypt(ticket));
 59                     Response.Cookies.Add(cookie);
 60 
 61                     return RedirectToAction("Index", "Account");
 62                 }
 63                 else
 64                 {
 65                     ModelState.AddModelError("", "用户名或密码不正确!");
 66                 }
 67             }
 68 
 69             return View(inputUser);
 70         }
 71         /// <summary>
 72         /// 退出登录
 73         /// </summary>
 74         /// <returns></returns>
 75         [HttpPost]
 76         public ActionResult LoginOff()
 77         {
 78             FormsAuthentication.SignOut();
 79             return RedirectToAction("Login", "Account");
 80         }
 81 
 82         //
 83         // GET: /Account/Details/5
 84 
 85         public ActionResult Details(int id = 0)
 86         {
 87             User user = account.GetUser(id);
 88             UserDetail userDetail = account.GetUserDetail(user.UserName);
 89             if (user == null)
 90             {
 91                 return HttpNotFound();
 92             }
 93             return View(userDetail);
 94         }
 95 
 96         //
 97         // GET: /Account/Create
 98 
 99         public ActionResult Create()
100         {
101             ViewBag.Password = "admin";
102             return View();
103         }
104 
105         //
106         // POST: /Account/Create
107 
108         [HttpPost]
109         [Authorize(Roles = "manager")]
110         public ActionResult Create(User user)
111         {
112             if (ModelState.IsValid)
113             {
114                 db.User.Add(user);
115                 UserDetail userDetail = new UserDetail { UserName = user.UserName, Email = "example@unbelts.com", RegisterDate = DateTime.Now };
116                 db.UserDetail.Add(userDetail);
117                 db.SaveChanges();
118                 //给用户加默认权限(职位):employee
119                 User_Role user_Role = new User_Role { UserID = db.User.FirstOrDefault(u => u.UserName == user.UserName).ID, RoleID = 1 };
120                 db.User_Role.Add(user_Role);
121                 db.SaveChanges();
122 
123                 return RedirectToAction("Index");
124             }
125 
126             return View(user);
127         }
128 
129         //
130         // GET: /Account/Edit/5
131 
132         public ActionResult Edit(int id = 0)
133         {
134             User user = db.User.Find(id);
135             if (user == null)
136             {
137                 return HttpNotFound();
138             }
139             return View(user);
140         }
141 
142         //
143         // POST: /Account/Edit/5
144 
145         [HttpPost]
146         public ActionResult Edit(User user)
147         {
148             if (ModelState.IsValid)
149             {
150                 db.Entry(user).State = EntityState.Modified;
151                 db.SaveChanges();
152                 return RedirectToAction("Index");
153             }
154             return View(user);
155         }
156 
157         //
158         // GET: /Account/Delete/5
159 
160         public ActionResult Delete(int id = 0)
161         {
162             User user = db.User.Find(id);
163             if (user == null)
164             {
165                 return HttpNotFound();
166             }
167             return View(user);
168         }
169 
170         //
171         // POST: /Account/Delete/5
172 
173         [HttpPost, ActionName("Delete")]
174         public ActionResult DeleteConfirmed(int id)
175         {
176             User user = db.User.Find(id);
177             db.User.Remove(user);
178             UserDetail userDetail = db.UserDetail.First(m => m.UserName == user.UserName);
179             db.UserDetail.Remove(userDetail);
180             User_Role user_Role = db.User_Role.Where(m => m.UserID == id) as User_Role;
181             if (user_Role != null)
182             {
183                 db.User_Role.Remove(user_Role);
184             }
185             db.SaveChanges();
186             return RedirectToAction("Index");
187         }
188 
189         //
190         //用户权限管理
191         public ActionResult UserRoleManager(int id = 0)
192         {
193             List<Role> roles = account.GetUserRolesList(id);          
194             if (roles == null)
195             {
196                 return HttpNotFound();
197             }
198             ViewBag.UserName = account.GetUser(id).UserName;
199             ViewBag.UserID=id;
200             return View(roles);
201         }
202         //
203         //给用户增加权限
204         public ActionResult AddRoleToUser(int userID = 0)
205         {
206             return View();
207         }
208         //
209         //POST:给用户增加权限
210         [HttpPost]
211         public ActionResult AddRoleToUser()
212         {
213             return View();
214         }
215         //
216         //删除用户权限
217         [HttpPost]
218         public ActionResult DeleteRoleToUser(int id = 0,int userID=0)
219         {
220 
221             if (account.DeleteUser_Role(userID,id))
222             {
223                 return RedirectToAction("UserRoleManager"); 
224             }
225             return View();
226         }
227         //
228         //用户管理
229         public ActionResult UserManager()
230         {
231             
232             return PartialView();
233         }
234         //
235         //POST: /Account/UserManager
236         //[HttpPost]
237         //public ActionResult UserManager()
238         //{
239         //    return View();
240         //}
241 
242         //显示权限列表
243         public ActionResult RoleList()
244         {
245             return PartialView(account.GetRoleList());
246         }
247         public ActionResult DeleteRole(int id = 0)
248         {
249             Role role = account.GetRole(id);
250             if (role == null)
251             {
252                 return HttpNotFound();
253             }
254             return PartialView(role);
255         }
256         [HttpPost, ActionName("DeleteRole")]
257         public ActionResult DeleteRoleConfirmed(int id)
258         {
259             if (account.DeleteRole(id))
260             {
261                 return RedirectToAction("RoleList");
262             }
263             else
264             {
265                 return PartialView();
266             }
267         }
268         //
269         //GET: /Account/CreateRole
270         public ActionResult CreateRole()
271         { 
272             return View(); 
273         }
274         //
275         //POST: /Account/CreateRole
276         [HttpPost]
277         public ActionResult CreateRole(Role role)
278         {
279             if (ModelState.IsValid)
280             {
281                 //添加权限(职位)
282                 if (account.CreateRole(role))
283                 {
284                     ModelState.AddModelError("", "添加成功");
285                     return PartialView(role);
286                 }
287                 else
288                 {
289                     ModelState.AddModelError("", "添加失败,请重试!");
290                 }
291             }
292             return PartialView(role);
293         }
294 
295         protected override void Dispose(bool disposing)
296         {
297             db.Dispose();
298             base.Dispose(disposing);
299         }
300     }
301 }

这个是我以前学的时候做的一段代码,没有做完,不过里边有些部分可以给你参考下,要具体一点的就用搜索一下“MVC中的权限验证”应该能找到很多。

Meen | 园豆:321 (菜鸟二级) | 2013-05-04 14:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册