2010-01-21 50 views
5

有沒有辦法可以在使用下面的命令字符串變量的作用....得到的用戶角色在一個字符串變量

System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent(); 
    System.Security.Principal.WindowsPrincipal wp = new System.Security.Principal.WindowsPrincipal(wi); 

我需要這個

FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,       // version 
                UserName.Text,   // user name 
                DateTime.Now,    // creation 
                DateTime.Now.AddMinutes(60),// Expiration 
                false,      // Persistent 
                role);   // User data 

as string role = wp.IsInRole();
但是這是不正確的

一些類似這樣的...

回答

1

像這樣嗎?

public static string FormsAuthUserData 
{ 
    get 
    { 
     IPrincipal principal = Thread.CurrentPrincipal; 
     if (principal == null) return null; 
     FormsIdentity identity = principal.Identity as FormsIdentity; 
     if (identity == null) return null; 
     FormsAuthenticationTicket ticket = identity.Ticket; 
     return ticket == null ? null : ticket.UserData; 
    } 
} 
6

您可以從WindowsIdentity.Groups屬性中獲取用戶屬於的組/角色列表。 WindowsIdentity.Groups集合僅包含用戶所在的組/角色的SID(IdentityReference的集合),但不包含組/角色的實際名稱。我將告訴你如何獲得用戶所在的所有組/角色的實際名稱。

首先,獲取WindowsIdentity對象。

WindowsIdentity identity = WindowsIdentity.GetCurrent(); 

二,使用LINQ翻譯的SID的(的IdentityReference)到NTACCOUNT的。

var groups = from sid in identity.Groups select sid.Translate(typeof(NTAccount)).Value; 

然後,您可以通過組循環並將其存儲在一個字符串數組,可以在的FormsAuthenticationTicket使用。這將使您獲得BUILTIN(本地計算機)組/角色以及用戶所在的域組/角色。

2

您似乎在混合蘋果和橘子。您是使用Windows還是表單身份驗證?

在這兩種情況下,您都可以從RoleProvider獲取用戶角色(如果已實施)。

檢查線程的當前主體只會公開一個檢查方法,如您所知,IsInRole,而角色提供程序將返回用戶所屬角色的字符串數組。

但是我不得不問,你爲什麼要在票中包裝一個角色?我唯一能看到的有效用例就是整合外部認證/角色孤島。

如果您更充分地解釋您的方案和要求,我相信我們可以爲您的問題找到具體的解決方案。

0

是的,表單身份驗證似乎與Windows身份衝突,但我寫了一些代碼,我相信會做你的問題。

首先,爲您的項目添加對System.DirectoryServices的引用。

您需要先初始化一個PrincipalContext對象。

imports System.DirectoryServices

Dim userImLookingFor as AccountManagement.UserPrincipal(ctx)
Dim tempUser As New AccountManagement.UserPrincipal(ctx)
tempUser.SamAccountName = p_samAccountName
Dim searcher As New AccountManagement.PrincipalSearcher(tempUser)
If searcher.FindAll().Count = 1 Then
userImLookingFor = searcher.FindAll()(0)

運行此代碼時,userImLookingFor包含由p_samAccountName指定的用戶。接下來,你想獲得組的列表。

Dim tempGp As New AccountManagement.GroupPrincipal(userImLookingFor.Context)
Dim searcher As New AccountManagement.PrincipalSearcher(tempGp)
Dim searchResult As AccountManagement.PrincipalSearchResult(Of AccountManagement.Principal)
searchResult = searcher.FindAll()

最後,你可能指的是信息搜索結果集合。要獲取組名,請通過索引枚舉並檢索「用戶主體名稱」或「SAM帳戶名稱」。

是的,表單身份驗證不能很好地與Active Directory一起玩,但讓我知道這是否有幫助。我對前一個答案中的方法不熟悉;這兩個不同的答案可能會爲您提供訪問不同功能的對象。

0

你可以對你的用戶類做一個擴展方法,以獲得系統中所有角色的集合(詢問你的角色提供者)做一個cicle(或使用linq)來詢問isInRole foreach角色並收集用戶角色在一個財產準備使用。

這可以是任何類型角色提供程序的通用方法。

4

爲了得到角色:http://msdn.microsoft.com/en-us/library/system.web.security.roles.getrolesforuser.aspx

使用Roles.GetRolesForUser()Roles.GetRolesForUser(Page.User.Identity.Name)來獲得當前用戶具有角色的數組。您可以指定要爲哪個用戶獲取角色Roles.GetRolesForUser("Specific UserName")

您可以使用String.Join(", ",Roles.GetRolesForUser())來獲取用戶具有的字符串。

String.Joinhttp://msdn.microsoft.com/en-us/library/57a79xd0.aspx

希望這有助於。

相關問題