Quantcast
Channel: Claims based access platform (CBA), code-named Geneva forum
Viewing all articles
Browse latest Browse all 2535

How to Register a custom SecurityTokenHandler with Code

$
0
0

Hi,

I've created a custom CustomUserNameSecurityTokenHandler an registered it within the configuration-file this way:

<system.identityModel><identityConfiguration><securityTokenHandlers><remove type="System.IdentityModel.Tokens.WindowsUserNameSecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/><add type="MiniSTS.CustomUserNameSecurityTokenHandler, MiniSTS"/></securityTokenHandlers></identityConfiguration></system.identityModel>

This works fine, but now I'm wondering how to do this using code. I've tried it with the following code, but it hasn't worked.

using (var host = new WSTrustServiceHost(new CustomSecurityTokenServiceConfiguration(issuerName, signingCertificateName, typeof(CustomSecurityTokenService)), new Uri("http://localhost:21000/FlugService_STS/Service.svc")))
{

    var cred = new ServiceCredentials();
    cred.UseIdentityConfiguration = true;


    var idConfig = cred.IdentityConfiguration;
    idConfig.SecurityTokenHandlers.Remove(idConfig.SecurityTokenHandlers.OfType<WindowsUserNameSecurityTokenHandler>().First());
    idConfig.SecurityTokenHandlers.AddOrReplace(new CustomUserNameSecurityTokenHandler());

    cred.ServiceCertificate.Certificate = CertificateUtil.GetCertificate(StoreName.My, StoreLocation.LocalMachine, "CN=STSTestCert");
    var debug = host.Description.Behaviors.OfType<ServiceDebugBehavior>().First();
    debug.IncludeExceptionDetailInFaults = true;

    var metadata = host.Description.Behaviors.OfType<ServiceMetadataBehavior>().First();
    metadata.HttpGetEnabled = true;

    host.Description.Behaviors.Add(cred);

    var binding = new WS2007HttpBinding();
    var mexBinding = MetadataExchangeBindings.CreateMexHttpBinding();

    var ep = host.AddServiceEndpoint(typeof(System.ServiceModel.Security.IWSTrust13SyncContract), binding, "IWSTrust13_UserName");

    var serviceAuth = host.Description.Behaviors.OfType<ServiceAuthorizationBehavior>().First();
    serviceAuth.PrincipalPermissionMode = PrincipalPermissionMode.Always;

    binding.Security.Mode = SecurityMode.Message;
    binding.Security.Message.EstablishSecurityContext = false;
            
    binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
          
    host.Open();


    Console.WriteLine("started");
    Console.ReadLine();
}

What's the problem here?

Wishes,

Manfred



Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>