[英]redirect to different pages based on user role in ASP.NET MVC 4 Internet Application Login
I'm creating ASP.NET MVC 4 Internet Application. 我正在创建ASP.NET MVC 4 Internet应用程序。 In that Application I created Login Page that any user can log, then I'm Trying to redirect user to different pages based on their role.
在该应用程序中,我创建了任何用户都可以登录的登录页面,然后我试图根据用户的角色将其重定向到其他页面。
ASP.NET Identity is the membership system here. ASP.NET Identity是这里的成员资格系统。
In my AspNetRoles Table I have two roles: 在我的AspNetRoles表中,我有两个角色:
Id| Name
1 | HEI_Admin
2 | HEI_User
This is my Login Controller method: 这是我的登录控制器方法:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
if (user.ConfirmedEmail == true)
{
await SignInAsync(user, model.RememberMe);
if (String.IsNullOrEmpty(returnUrl))
{
if (UserManager.IsInRole(user.Id, "HEC_Admin"))
{
return RedirectToAction("Index", "HEC");
}
//role Admin go to Admin page
if (UserManager.IsInRole(user.Id, "HEI_User"))
{
return RedirectToAction("Index", "HEI");
}
}
else
{
return RedirectToLocal(returnUrl);
}
}
else
{
ModelState.AddModelError("", "Confirm Email Address.");
}
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
but when I try to log using correct credentials I'm getting following error: 但是当我尝试使用正确的凭据登录时,出现以下错误:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible.
服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
验证实例名称正确,并且已将SQL Server配置为允许远程连接。
All the connection are double checked, how can achieve this problem. 所有连接都经过仔细检查,如何才能解决这个问题。
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
if (UserManager.IsInRole(user.Id, "Admin"))
{
return RedirectToAction("Index", "Admin");
}
else if (UserManager.IsInRole(user.Id, "Clerk"))
{
return RedirectToAction("Index","Employee");
}
else if (UserManager.IsInRole(user.Id, "Customer"))
{
return RedirectToAction("Index", "Customer");
}
//return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("","Invalid username or password");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
This is the code i used for redirect. 这是我用于重定向的代码。 also see if the userID and RoleID are inserted into the ASPNETUserRoles table
还查看是否将userID和RoleID插入到ASPNETUserRoles表中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.