在WCF服务中使用自定义主体和标识的问题

我们使用自定义主体和标识类型(ProdigyPrincipal/ProdigyIdentity),因为我们需要在我们的程序和服务中提供额外的信息。在该计划中,我们设置了校长和身份。在与WCF服务进行通信时,将设置主体和标识,但在投射到我们自己的类型后,主体和标识为空。在WCF服务中使用自定义主体和标识的问题

我注意到在调试模式和单元测试模式下运行有区别。在调试模式下,主体和标识的类型为WindowsPrincipalWindowsIdentity。在单元测试模式下,类型是GenericPrincipal和GenericIdenity。在转换为自定义类型的两种情况下,该值都为空。

通过Thread.CurrentPrincipal完成主体/身份的设置和获取。 在绑定部分的App.configs中,安全模式设置为'Transport'。

使用功能设置/获取本金和身份:

protected static bool SetProdigyPrincipal() 

{

#region require Thread.CurrentPrincipal should not be null

if (Thread.CurrentPrincipal == null) // OK

{

throw new InvalidOperationException("SetProdigyPrincipal(): Thread.CurrentPrincipal should not be null");

}

#endregion require Thread.CurrentPrincipal should not be null

var prodigyPrincipal = Thread.CurrentPrincipal as ProdigyPrincipal;

#region require prodigyPrincipal should not be null

if (prodigyPrincipal == null) // NOT OK

{

throw new InvalidOperationException("SetProdigyPrincipal(): prodigyPrincipal should not be null");

}

#endregion require prodigyPrincipal should not be null

// Get the Windows identity from the current principal

var prodigyIdentity = Thread.CurrentPrincipal.Identity as ProdigyIdentity;

#region require windowsIdentity should not be null

if (prodigyIdentity == null) // NOT OK

{

throw new InvalidOperationException("SetProdigyPrincipal(): prodigyIdentity should not be null");

}

#endregion require windowsIdentity should not be null

// Create new instance of Prodigy principal

var newProdigyPrincipal = new ProdigyPrincipal(prodigyIdentity);

#region require prodigyPrincipal should not be null

if (prodigyPrincipal == null)

{

throw new InvalidOperationException("SetProdigyPrincipal(): prodigyPrincipal should not be null");

}

#endregion require prodigyPrincipal should not be null

// Set the prodigy principal

var principalIsSet = ProdigyPrincipal.SetCurrentPrincipal(newProdigyPrincipal, ProdigyService.EnterpriseServiceBus);

// Return principal is set status

return principalIsSet;

}

有谁知道为什么自定义主体和身份类型不能从主题检索?

类方面,汉斯

回答:

WCF具有实现相同目标的更标准的方式,通过ServiceAuthorizationBehavior。

如果将其PrincipalPermissionMode属性设置为“Custom”,它允许您提供自定义IAuthorizationPolicy,通过该自定义IPrincipal可以使用WCF ServiceSecurityContext的自定义IPrincipal。 DispatchRuntime会将此(您的自定义)IPrincipal分配给Thread.CurrentPrincipal - 这就是您所要的,对吧?

这是一个示例IAuthorizationPolicy实现:

public class DemoAuthorizationPolicy : IAuthorizationPolicy 

{

private readonly string id = Guid.NewGuid().ToString();

public string Id { get { return this.id; } }

public ClaimSet Issuer { get { return ClaimSet.System; } }

public bool Evaluate(EvaluationContext context, ref object state)

{

// Here, create your custom principal

IIdentity customIdentity = new GenericIdentity("myUserName", "myCustomAuthenticationType");

IPrincipal customPrincipal = new GenericPrincipal(customIdentity, new[] { "user", "powerUser" });

// Set EvaluationContext properties

context.Properties["Identities"] = new List<IIdentity> { customIdentity };

context.Properties["Principal"] = customPrincipal;

return true;

}

}

而且你这是怎么声明ServiceAuthorizationBehavior在Web.config:

<system.serviceModel> 

<behaviors>

<serviceBehaviors>

<behavior>

<serviceAuthorization principalPermissionMode="Custom" >

<authorizationPolicies>

<add policyType="PrincipalPermissionModeDemo.DemoAuthorizationPolicy, YourAssemblyName"/>

</authorizationPolicies>

</serviceAuthorization>

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

里面您服务,您可以利用,然后通过声明式安全[PrincipalPermission]属性,您可以从Thread.CurrentPrincipal获得自定义IPrincipal,(或者)您也可以获得自定义IIdentity from ServiceSecurityContext.Current.PrimaryIdentity

希望能解决您的问题!

以上是 在WCF服务中使用自定义主体和标识的问题 的全部内容, 来源链接: utcz.com/qa/266438.html

回到顶部