3
我需要實現一個單一登錄,以在應用程序中調用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