3

如何从收到的 msmq 消息中获取发件人的 WindowsIdentity?

我使用 msmq 作为传输和带有授权规则提供程序的安全应用程序块来进行操作授权。我需要 WindowsPrincipal 而不是 GenericPrincipal 因为规则授予活动目录用户组而不是特定用户。Message.SenderId 可以转换为 SecurityIdentifier 但我没有找到如何从中获取 WindowsIdentity 。

void AuthorizeOperation(Message message)
{
   // get sender windows principal
   WindowsPrincipal principal = ... ???

   // extract operation name from message body
   string operation = ... 

   AuthorizationFactory.GetAuthorizationProvider().Authorize(principal, operation);
}
4

1 回答 1

0

我找到了一种解决方法,但不确定它是否是正确的解决方案。我创建了一个 GenericPrincipal 并注入从活动目录收到的用户授权组,而不是 WindowsPrincipal。

var sid = new SecurityIdentifier(message.SenderId, 0);
var user = UserPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain), IdentityType.Sid, sid);
var principal = new GenericPrincipal(
                   new GenericIdentity(user.SamAccountName),
                   user.GetAuthorizationGroups().Select(g => g.SamAccountName).ToArray());
bool authorized = AuthorizationFactory.GetAuthorizationProvider().Authorize(principal, operation);
于 2011-11-01T09:20:37.563 回答