2014-02-28 69 views
0

我的應用程序使用Windows授權,但我手動指定有這樣的訪問權的用戶:Windows身份驗證來獲得用戶的列表中MVC4

[Authorize(Users = "domain\\userone, domain\\usertwo, domain\\userthree")] 

我不知道我是否可以把一個循環,在那裏通過來自數據庫調用返回時,用戶列表循環,使例如一個快速模仿起來會從數據庫中獲取的用戶的NT帳戶列表:

List<string> users = new List<String>(); 

SqlConnection con = new SqlConnection(Properties.Default.ConnectionString); 
SqlCommand cmd = new SqlCommand(); 
cmd.Connection = con; 
cmd.CommandType = System.Data.CommandType.Text; 
cmd.CommandText = @"SELECT NT_ACCOUNT FROM USERS"; 

SQLDataReader reader = cmd.ExecuteReader(); 
while(reader.Read()) 
{ 
    users.Add(reader.GetValue(reader.FieldCount)); 
} 

,從我認爲一個實物模型它會看起來像授權:

foreach(String nt_account in users) 
{ 
[Authorize(Users = nt_account)] 
} 

或者也許它可能更容易與linq查詢我不知道,上面的代碼只是一個猜測。

+0

:從

protected override bool AuthorizeCore(HttpContextBase httpContext) { if (httpContext == null) { throw new ArgumentNullException("httpContext"); } IPrincipal user = httpContext.User; if (!user.Identity.IsAuthenticated) { return false; } //_usersSplit = ListOfAuthorizedNames if ((_usersSplit.Length > 0 && !_usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase)) && (_rolesSplit.Length > 0 && !_rolesSplit.Any(user.IsInRole))) { return false; } return true; } 

多久? –

+0

@Sunny由於用戶列表已經是一個表,但並不是該列表中的每個人都應該被允許訪問,因此在門戶中允許或禁止該訪問的某些功能會很好。 – user2405469

+0

好吧,你需要修改AuthorizeCore方法然後,看到這個鏈接:http://stackoverflow.com/a/6426328/1057667 –

回答

相關問題