Sergey Shishkin

on agile software development

CardSpace SDK: Creating Managed Cards, Part 2

Ok, instead of writing a long story I’ll just post two samples from the upcoming SDK pre-release:


The “Simplest” Information Card

In this sample you will be introduced to the Information Card API and the newtelligence.CardSpace.InformationCard class it provides.

To create a managed information card simply instantiate the InformationCard class using its default constructor, set the required properties and call one of the Save method’s overloads.

Source Code

    InformationCard card = new InformationCard();

 

    // Specify a certificate to sign the card

    card.SetSigningCertificate(

        StoreLocation.LocalMachine, StoreName.My,

        X509FindType.FindBySubjectName, "www.fabrikam.com");

 

    // Set the card’s Id and the issuer

    card.CardId = new Uri("http://cardspace.newtelligence.com/cards/1");

    card.Issuer = new Uri("http://cardspace.newtelligence.com/ip");

 

    // Specify a security token service for the card

    TokenServiceReference sts = new TokenServiceReference();

    sts.SetEndpointReference(

        // The endpoint address of the STS

        new Uri("http://cardspace.newtelligence.com/sts"),

        // The same certificate is used as an identity of the STS

        card.SigningCertificate,

        // The MetadataExchange endpoint of the STS (SSL is required)

        new Uri("https://cardspace.newtelligence.com/sts/mex"));

    // The STS uses a username-password authentication

    sts.UserCredential = new UsernamePasswordCredential();

    card.TokenServices.Add(sts);

 

    // Specify a type of security tokens supported by the card

    card.TokenTypes.Add(new Uri(

        "urn:oasis:names:tc:SAML:1.0:assertion"));

    // Specify a type of claims supported by the card

    card.ClaimTypes.Add(new ClaimType(

        "http://cardspace.newtelligence.com/claims/custom-claim"));

 

    card.Save("card.crd");


Working with Cards

This sample uses the code from the previous sample to create and save a simple managed card. Then a new instance of the InformationCard class is used to load a previously created card and change its properties. The sample code sets the name and specifies an image for the card as well as other properties. In addition it changes the Security Token Service to be used with the card, and the claims that the card supports. Finally, the card is saved to the file with the same name.

Source Code

    InformationCard card = new InformationCard();

 

    // Load an existing card

    card.Load("card.crd");

 

    // Change properties’ values

    card.CardVersion++;

    card.CardName = "newtelligence";

    card.SetImageFromFile("card.png");

    card.TimeExpires = card.TimeIssued.AddYears(1);

    card.RequireAppliesTo = true;

    card.PrivacyNotice = new Uri(

        "http://cardspace.newtelligence.com/ip/privacy");

    card.PrivacyNoticeVersion = 3;

 

    // Token services

    card.TokenServices.Clear();

 

    TokenServiceReference sts = new TokenServiceReference();

    sts.SetEndpointReference(

        new Uri("http://cardspace.newtelligence.com/sts/kerberos"),

        card.SigningCertificate,

        new Uri("https://cardspace.newtelligence.com/sts/mex"));

    sts.UserCredential = new KerberosV5Credential();

    card.TokenServices.Add(sts);

 

    // Claims

    card.ClaimTypes.Clear();

 

    ClaimType claimName = new ClaimType(

        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname");

    claimName.DisplayTag = "Last Name";

    claimName.Description = "Surname or family name of a subject";

    card.ClaimTypes.Add(claimName);

 

    ClaimType claimCustom = new ClaimType(

        "http://cardspace.newtelligence.com/claims/custom");

    claimCustom.DisplayTag = "Custom Claim";

    claimCustom.Description = "This is a custom claim";

    card.ClaimTypes.Add(claimCustom);

 

    // Save the card to the same file

    card.Save("card.crd");

Advertisements

Written by Sergey Shishkin

08.05.2007 at 09:46

Posted in Uncategorized

%d bloggers like this: