本文整理汇总了C#中System.IdentityModel.Claims.ClaimTypes类的典型用法代码示例。如果您正苦于以下问题:C# ClaimTypes类的具体用法?C# ClaimTypes怎么用?C# ClaimTypes使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ClaimTypes类属于System.IdentityModel.Claims命名空间,在下文中一共展示了ClaimTypes类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Echo
//引入命名空间
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.IdentityModel.Claims;
using System.IdentityModel.Policy;
using System.IdentityModel.Tokens;
using System.IdentityModel.Selectors;
using System.ServiceModel;
namespace Microsoft.ServiceModel.Samples.SupportingTokens
{
[ServiceContract]
public interface IEchoService : IDisposable
{
[OperationContract]
string Echo();
}
// Service class that implements the service contract.
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class EchoService : IEchoService
{
public string Echo()
{
string userName;
string certificateSubjectName;
GetCallerIdentities(OperationContext.Current.ServiceSecurityContext, out userName, out certificateSubjectName);
return String.Format("Hello {0}, {1}", userName, certificateSubjectName);
}
public void Dispose()
{
}
bool TryGetClaimValue<TClaimResource>(ClaimSet claimSet, string claimType, out TClaimResource resourceValue)
where TClaimResource : class
{
resourceValue = default(TClaimResource);
IEnumerable<Claim> matchingClaims = claimSet.FindClaims(claimType, Rights.PossessProperty);
if (matchingClaims == null)
return false;
IEnumerator<Claim> enumerator = matchingClaims.GetEnumerator();
if (enumerator.MoveNext())
{
resourceValue = (enumerator.Current.Resource == null) ? null : (enumerator.Current.Resource as TClaimResource);
return true;
}
else
{
return false;
}
}
// Returns the username and certificate subject name provided by the client.
void GetCallerIdentities(ServiceSecurityContext callerSecurityContext, out string userName, out string certificateSubjectName)
{
userName = null;
certificateSubjectName = null;
// Look in all the claimsets in the authorization context.
foreach (ClaimSet claimSet in callerSecurityContext.AuthorizationContext.ClaimSets)
{
// Try to find a Upn claim. This has been generated from the windows username.
string tmpName;
if (TryGetClaimValue<string>(claimSet, ClaimTypes.Upn, out tmpName))
{
userName = tmpName;
}
else
{
// Try to find an X500DistinguishedName claim. This has been generated from the client certificate.
X500DistinguishedName tmpDistinguishedName;
if (TryGetClaimValue<X500DistinguishedName>(claimSet, ClaimTypes.X500DistinguishedName, out tmpDistinguishedName))
{
certificateSubjectName = tmpDistinguishedName.Name;
}
}
}
}
}
}