一度私はPrimaryIdentityにUserNameと共にパスワードを保存しようとしました。 これを達成するには、UserNameとPasswordを認証してIdentityに格納し、IdentityをWCFのSecurityContextに格納する新しいUserNameSecurityTokenAuthenticatorを提供する必要があります。
ステップ
クラス
1を実行してくださいする)TestServiceHost:のServiceHost
2)UserNamePasswordSecurityTokenManager:ServiceCredentialsSecurityTokenManager
3)TestUserNameSecurityTokenAuthenticator:UserNameSecurityTokenAuthenticator
4)MyIdentity :IIdentity
5)MyAuthorizatoinPolicy:IAuthorizationPolicy
1)が続い)
2)TestServiceHostでOnOpeningをオーバーライドTestServiceHost新しいのServiceHostクラスを作成し、新しいクラスUserNamePasswordServiceCredentials
3.
protected override void OnOpening()
{
base.OnOpening();
this.Description.Behaviors.Add(new UserNamePasswordServiceCredentials());
}
に提供しますUserNamePasswordServiceCredentialsで新しいUserNamePasswordSecurityTokenManagerを指定します。
public override SecurityTokenManager CreateSecurityTokenManager()
{
return new UserNamePasswordSecurityTokenManager(this);
}
4)次にUserNamePasswordSecurityTokenManagerに新しいTestUserNameSecurityTokenAuthenticatorに
public override SecurityTokenAuthenticator CreateSecurityTokenAuthenticator(SecurityTokenRequirement tokenRequirement, out SecurityTokenResolver outOfBandTokenResolver)
{
if (tokenRequirement.TokenType == SecurityTokenTypes.UserName)
{
outOfBandTokenResolver = null;
return new TestUserNameSecurityTokenAuthenticator();
}
return base.CreateSecurityTokenAuthenticator(tokenRequirement, out outOfBandTokenResolver);
}
5.を提供)次にTestUserNameSecurityTokenAuthenticator内には、UseraNameとパスワードを認証することができますし、独自のアイデンティティを作成することができます。この機能では、評価されるIAuthorizationポリシーのリストを返します。私は独自の認可ポリシーを作成し、それに新しいアイデンティティーを渡したので、アイデンティティをコンテキストに設定します。
protected override System.Collections.ObjectModel.ReadOnlyCollection<System.IdentityModel.Policy.IAuthorizationPolicy> ValidateUserNamePasswordCore(string userName, string password)
{
ClaimSet claimSet = new DefaultClaimSet(ClaimSet.System, new Claim(ClaimTypes.Name, userName, Rights.PossessProperty));
List<IIdentity> identities = new List<IIdentity>(1);
identities.Add(new MyIdentity(userName,password));
List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(1);
policies.Add(new MyAuthorizationPolicy(ClaimSet.System, identities));
return policies.AsReadOnly();
}
public class MyAuthorizationPolicy : IAuthorizationPolicy
{
String id = Guid.NewGuid().ToString();
ClaimSet issuer;
private IList<IIdentity> identities;
#region IAuthorizationPolicy Members
public MyAuthorizationPolicy(ClaimSet issuer, IList<IIdentity> identities)
{
if (issuer == null)
throw new ArgumentNullException("issuer");
this.issuer = issuer;
this.identities = identities;
}
public bool Evaluate(EvaluationContext evaluationContext, ref object state)
{
if (this.identities != null)
{
object value;
IList<IIdentity> contextIdentities;
if (!evaluationContext.Properties.TryGetValue("Identities", out value))
{
contextIdentities = new List<IIdentity>(this.identities.Count);
evaluationContext.Properties.Add("Identities", contextIdentities);
}
else
{
contextIdentities = value as IList<IIdentity>;
}
foreach (IIdentity identity in this.identities)
{
contextIdentities.Add(identity);
}
}
return true;
}
public ClaimSet Issuer
{
get { return this.issuer; }
}
#endregion
#region IAuthorizationComponent Members
public string Id
{
get { return this.id; }
}
#endregion
}
したがって、この例では、WCFでセキュリティを無効にする方法を示しています。
今すぐあなたの問題で:
1)この技術と設定したユーザ名とあなたのアイデンティティでパスワードを実装します。今すぐあなたが子サービスを呼び出すと、アイデンティティはそこからユーザー名とパスワードを抽出し、子サービスに渡します。
2.)ユーザー名とパスワードを認証し、トークンを生成します(トークンサービスを新規作成する必要があります)。あなたのアイデンティティにこのトークンをユーザー名と共に保存し、これらの2つをあなたの子サービスに渡します。このアプローチが有効になると、子サービスは新しい生成トークンを検証しなければなりません。このため、ユーザー名とパスワードを検証してトークンを作成できるトークンサービスが必要です。
個人的に私はアプローチ2に行きますが、新しいオーバーヘッドが導入されます。
こんにちは、お返事ありがとうございます。 1.現在のところパフォーマンスの問題はありませんが、将来はTCP/IPCバインディングを使用するように変更する場合はテストします。 コードクラリティについては、このファサードサービスがこの目的のために設計され、作られているため、全く問題はありません。 2. UserName資格情報を使用し、UserName/Passwordが必要なので不可能です。 3.偽装はWindowsのみで動作し、UserName/Password資格情報タイプが必要です。おかげさまで –