我正在努力学习使用身份验证& MVC和实体框架中的身份安全性存在一些困难。
我正在开发一个大学网络应用程序,而我遇到的问题是将将在应用程序中注册的新用户连接为不同的角色,如学生或讲师。我已经在我的Seed方法,Admin,Student和Instructor中创建了3个角色。
如何在 AccountController 中编写Register
方法来连接Student表和ApplicationUser表?
或者当学生登录时,他只能看到他的帐户,而不是学生页面中的学生列表?
我使用 ApplicationUser 表和 Person 表创建了一个外键(Student和Instructor表继承自Person表)。
public abstract class Person
{
public int ID {get; set;}
[ForeignKey("ApplicationUser")]
public string ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
AccountController.cs
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
//Assign Role to User
await this.UserManager.AddToRoleAsync(user.Id, model.UserRoles);
return RedirectToAction("Index", "Home");
}
ViewBag.Name = new SelectList(context.Roles.Where(u => !u.Name.Contains("Admin"))
.ToList(), "Name", "Name");
AddErrors(result);
}
// Register Student
RegisterViewModel
public class RegisterViewModel
{
[Required]
[Display(Name = "UserRoles")]
public string UserRoles { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
//[Required]
[Display(Name = "Username")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
目前,学生用户可以看到所有人,但我希望学生只能看到他的帐户信息。请注意每个人的信息。
Configuration.cs (种子方法)
protected override void Seed(SchoolContext context)
{
var students = new List<Student>
{
new Student { FirstMidName = "Carson", LastName = "Alexander",
EnrollmentDate = DateTime.Parse("2010-09-01") },
new Student { FirstMidName = "Meredith", LastName = "Alonso",
EnrollmentDate = DateTime.Parse("2012-09-01") },
new Student { FirstMidName = "Arturo", LastName = "Anand",
EnrollmentDate = DateTime.Parse("2013-09-01") },
new Student { FirstMidName = "Gytis", LastName = "Barzdukas",
EnrollmentDate = DateTime.Parse("2012-09-01") },
new Student { FirstMidName = "Yan", LastName = "Li",
EnrollmentDate = DateTime.Parse("2012-09-01") },
new Student { FirstMidName = "Peggy", LastName = "Justice",
EnrollmentDate = DateTime.Parse("2011-09-01") },
new Student { FirstMidName = "Laura", LastName = "Norman",
EnrollmentDate = DateTime.Parse("2013-09-01") },
new Student { FirstMidName = "Nino", LastName = "Olivetto",
EnrollmentDate = DateTime.Parse("2005-09-01") }
};
students.ForEach(s => context.Students.AddOrUpdate(p => p.LastName, s));
context.SaveChanges();
//Adding users
//if (!context.Users.Any(u => u.UserName == "Car"))
//{
// var store = new UserStore<ApplicationUser>(context);
// var manager = new UserManager<ApplicationUser>(store);
// var user = new ApplicationUser { UserName = "sampleTwo@email.com", Email = "sampleTwo@email.com" };
// manager.Create(user, "password123");
//}
我已经搜索了很多内容,但无法提供自定义注册方法来处理不同的表格。任何帮助都会有所帮助。
答案 0 :(得分:2)
您可以根据用户角色更改SQL命中。
#include <iostream>
int main(int argc, char* argv[])
{
std::cout.setf( std::ios_base::unitbuf ); //instead of "<< eof" and "flushall"
unsigned int a, c, i, t=0;
std::string inp;
bool bCommunicationEnds = false;
do {
inp="";
t=0;
// Sum the first 4 chars from stdin (the length of the message passed).
for (i = 0; i <= 3; i++)
{
// t += getchar();
t += std::cin.get();
}
// Loop getchar to pull in the message until we reach the total
// length provided.
for (i=0; i < t; i++)
{
//c = getchar();
c = std::cin.get();
//if(c == EOF)
if(c == 65)
{
bCommunicationEnds = true;
i = t;
}
else
{
inp += c;
}
}
if(!bCommunicationEnds)
{
//Collect the length of the message
unsigned int len = inp.length();
//unsigned int len = strJson.length();
//// We need to send the 4 btyes of length information
std::cout << char(((len>>0) & 0xFF))
<< char(((len>>8) & 0xFF))
<< char(((len>>16) & 0xFF))
<< char(((len>>24) & 0xFF));
//// Now we can output our message
std::cout << inp;
}
}while(!bCommunicationEnds);
return 0;
}
如果用户不是管理员,则只返回他们的记录。