[Team System] TeamFoundationServerFactory e authentication

Attenzione quando usate l'oggetto TeamFoundationServerFactory: sebbene permetta di riutilizzare da più oggetti TeamFoundationServer la stessa connessione al server (e quindi risparmiare un po' di risorse) ha un comportamento non molto intuitivo legato all'autenticazione.

premesso using Microsoft.TeamFoundation.Client; e una semplice classe CredentialProvider (che non includo perché il post mi sembra già troppo lungo) che ritorna user e password alla richiesta di TFS.

Gli esempi seguenti dovrebbero creare due oggetti autenticati rispettivamente come user1 e user2

TeamFoundationServer tfs1 = TeamFoundationServerFactory.GetServer("http://tfsrtmsp1:8080", new CredentialProvider("user1", "pass1"));
tfs1.Authenticate();
TeamFoundationServer tfs2 = TeamFoundationServerFactory.GetServer("http://tfsrtmsp1:8080", new CredentialProvider("user2", "pass2"));
tfs2.Authenticate();
Console.WriteLine("tfs1: " + tfs1.AuthenticatedUserName + "\ntfs2: " + tfs2.AuthenticatedUserName);
E' facile vedere che in realtà sia tfs1 che tfs2 sono autenticati come user1 (non solo, ma condividono anche lo stesso SessionId mentre dovrebbero essere distinti)!

Cosa che invece non accade se "saltiamo" la Factory:
TeamFoundationServer tfs1 = new TeamFoundationServer("http://tfsrtmsp1:8080", new CredentialProvider("user1", "pass1"));
tfs1.Authenticate();
TeamFoundationServer tfs2 = new TeamFoundationServer("http://tfsrtmsp1:8080", new CredentialProvider("user2", "pass2"));
tfs2.Authenticate();
Console.WriteLine("tfs1: " + tfs1.AuthenticatedUserName + "\ntfs2: " + tfs2.AuthenticatedUserName);

E' comunque possible utilizzare la Factory con due utenti diversi a patto che venga fatto il Dispose dell'oggetto precedentemente creato dalla Factory prima di creare il secondo:
TeamFoundationServer tfs1 = TeamFoundationServerFactory.GetServer("http://tfsrtmsp1:8080", new CredentialProvider("user1", "pass1"));
tfs1.Authenticate();
tfs1.Dispose();
TeamFoundationServer tfs2 = TeamFoundationServerFactory.GetServer("http://tfsrtmsp1:8080", new CredentialProvider("user2", "pass2"));
tfs2.Authenticate();

Console.WriteLine("tfs1: " + tfs1.AuthenticatedUserName + "\ntfs2: " + tfs2.AuthenticatedUserName);
Da notare ulteriormente che la proprietà AuthenticatedUserName rimane invocabile anche se l'oggetto é stato disposto.

Ovviamente (ma non includo l'ennesimo codice) se create tfs1 con new e tfs2 con TeamFoundationServerFactory non avrete nessun problema.

Print | posted on lunedì 3 settembre 2007 17:29

Comments have been closed on this topic.