我需要实现一个单一登录,以在应用程序中调用Com +组件来验证用户&提供的角色。总之,我需要绕过MVC 4中的默认机制,尝试访问aspnetdb数据库。所以我开始了一个新的MVC4互联网项目,并添加了下面的代码。MVC 4中的自定义授权和角色不起作用
在Global.asax中
public void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs args)
{
bool retval = CreateUserObject("John", "pwd");
}
private bool CreateUserObject(string userName, string password)
{
string[] currentUserRoles = { "Admin", "User" };
GenericPrincipal userPrincipal = new GenericPrincipal(new GenericIdentity(userName), currentUserRoles);
HttpContext.Current.User = userPrincipal;
//Thread.CurrentPrincipal = userPrincipal;
return true;
}
的HomeController.cs内,我加入了[授权]属性的 “关于”,如下行动,它将按预期工作
[Authorize]
public ActionResult About()
但是,如果我修改[授权]属性只允许“管理”角色如下,我得到一个运行时错误(在底部)。有没有办法解决这个问题,以便为登录用户使用我自己的角色集合,而不是查询数据库?我也要做类似的用户配置文件的东西,以及(即,而不是数据库,我应该从COM +应用填充值。
[Authorize(Roles = "Admin")]
public ActionResult About()
服务器错误“/”应用。
与SQL Server建立连接时发生网络相关或特定于实例的错误服务器未找到或无法访问验证实例名称是否正确以及SQL Server是否配置为允许远程连接(提供程序:SQL网络接口,错误:26 - 错误定位服务器/实例指定)
为什么不创建一个自定义授权属性? – 2013-02-28 16:24:08