のようにweb.configファイルに結合定義します。以下のようにカスタムの認証および承認のクラスを提供すると
<behavior name="Namespace.TestBehaviour">
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="Namespace.ServiceSecurity.UserAuthenticator, Namespace" />
</serviceCredentials>
<serviceAuthorization>
<authorizationPolicies>
<add policyType="Namespace.ServiceSecurity.MyAuthorizationPolicy, Namespace" />
</authorizationPolicies>
</serviceAuthorization>
</behavior>
:
その後のようなサービスの動作を定義
<basicHttpBinding>
<binding name="BasicAuthBinding">
<security mode="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</basicHttpBinding>
を
public class MyAuthorizationPolicy: IAuthorizationPolicy
{
public bool Evaluate(EvaluationContext evaluationContext, ref object state)
{
IList<IIdentity> identities = (IList<IIdentity>) evaluationContext.Properties["Identities"];
foreach (IIdentity identity in identities)
{
if (identity.IsAuthenticated &&
identity.AuthenticationType == "UserAuthenticator")
{
evaluationContext.Properties["Principal"] = identity.Name;
return true;
}
}
if (!evaluationContext.Properties.ContainsKey("Principal"))
{
evaluationContext.Properties["Principal"] = "";
}
return false;
}
public ClaimSet Issuer
{
get { throw new NotImplementedException(); }
}
}
認証:
public class UserAuthenticator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
//authenticate me however you want
//then set whatever you want
}
}
あなたは、さらにセキュリティが必要な場合は、wsHttpBindingにbasicHttpBindingに変更し、証明書
EDITを使用するには:ほとんど定義されたサービスの動作を使用し、忘れてweb.configファイルでサービス・インターフェース定義に結合します。コンフィグで
public class WCF_Project_Authentification : UserNamePasswordValidator
{
#region UserNamePasswordValidator Interface Member
public override void Validate(string userName, string password)
{
if (userName != "Jeanc" || password != "fortin")
{
throw new FaultException("Authentification failed");
}
}
#endregion
}
:
<behaviors>
<serviceBehaviors>
<behavior name="Service_Behavior">
<serviceMetadata httpGetEnabled="False" httpsGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="WcfService.Authentification.WCF_Project_Authentification, WcfService"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
そして、クライアントコードで:コードで
>ありがとうございます!これを実装しましたが、サービスをブラウズするときに次の例外が発生しました。BasicHttpバインディングでは、BasicHttpBinding.Security.Message.ClientCredentialTypeが、セキュリティで保護されたメッセージのBasicHttpMessageCredentialType.Certificate資格情報タイプと同等である必要があります。 UserName資格情報の転送またはTransportWithMessageCredentialセキュリティを選択します。 – Banshee
>私はこれへの結合を設定しようとしています <輸送clientCredentialTypeは=「基本」><バインディング名=「BasicAuthIntegration」> <セキュリティモード=「TransportCredentialOnly」> 、これはサービスが軌道に乗るだろうさ(ClientCredentials.UserName.UserNameとClientCredentials.UserName.Passwordを使用して)testclientからサービスを呼び出そうとすると、(クライアントで)この例外が発生します。クライアント認証スキーム「Basic」でHTTPリクエストが不正です。サーバーから受信した認証ヘッダーは 'Basic realm = "localhost"でした。 – Banshee