Answered by:
From CredentialPicker to HttpClient Basic Authentication

Question
-
Hello,
I was playing around with the CredentialPicker and HttpClient.
What I want to do is to:
- use a CredentialPicker to get a username (in form of an email) and a password.
- store the username/password (maybe using a password vault or anything else, any idea is welcome)
- use the username/password to connect to a REST service using HttpClient and BASIC Authentication.
A plus, would be to have the CredentialPicker being able to check the login on the website using the BASIC Authentication.
Any Help is wellcome (I know how to open the credential picker, but where to store the username? How to use the encrypted password for Basic Authentication?)
Thanks!
Monday, June 4, 2012 8:29 PM
Answers
-
1) Create a CredentialPickerOptions object.
2) Set the object's AuthenticationProtocol property to Basic, other properties as needed
3) Use CredentialPicker.PickAsync(CredentialPickerOptions) to get the user creds
4) Get the CredentialUserName and CredentialPassword (this will be clear-text) from the CredentialPickerResults object returned by PickAsync
5) See the Credential Locker sample (http://code.msdn.microsoft.com/windowsapps/PasswordVault-f01be74a) to store the username/pwd
6) Use System.net.HttpClient to call your REST service. Sample: http://code.msdn.microsoft.com/windowsapps/HttpClient-sample-55700664
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.- Proposed as answer by Matt SmallMicrosoft employee, Moderator Monday, June 4, 2012 11:04 PM
- Marked as answer by SirLeamont Wednesday, June 6, 2012 9:12 AM
Monday, June 4, 2012 11:04 PMModerator -
About the Password vault, all is ok. (BTW, another app using the same resources may be able to retrieve the username/password? or the vault is app dépendent?
The Http Client sample don't show how to define credentials, I have setted the
HttpClient.DefaultRequestHeaders.Authorization
But I have found no helpers to create BasicAuth. I have created it manually with the following code:string basic = Username + ":" + Password; List<byte> arr = new List<byte>(); foreach (var c in basic) // ASCII encoding not present { arr.Add((byte)c); } var base64 = Convert.ToBase64String(arr.ToArray()); return new AuthenticationHeaderValue("Basic", base64);
Any better (more integrated) solution?
- Marked as answer by SirLeamont Wednesday, June 6, 2012 9:13 AM
Tuesday, June 5, 2012 3:51 PM
All replies
-
1) Create a CredentialPickerOptions object.
2) Set the object's AuthenticationProtocol property to Basic, other properties as needed
3) Use CredentialPicker.PickAsync(CredentialPickerOptions) to get the user creds
4) Get the CredentialUserName and CredentialPassword (this will be clear-text) from the CredentialPickerResults object returned by PickAsync
5) See the Credential Locker sample (http://code.msdn.microsoft.com/windowsapps/PasswordVault-f01be74a) to store the username/pwd
6) Use System.net.HttpClient to call your REST service. Sample: http://code.msdn.microsoft.com/windowsapps/HttpClient-sample-55700664
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.- Proposed as answer by Matt SmallMicrosoft employee, Moderator Monday, June 4, 2012 11:04 PM
- Marked as answer by SirLeamont Wednesday, June 6, 2012 9:12 AM
Monday, June 4, 2012 11:04 PMModerator -
Just another question, I previously wrote this by creating my "own" credentials inputs on a config pane (like it was done in consumer preview for mail, for instance).
Will this be a blocker for App Certification for Windows Store?
Tuesday, June 5, 2012 2:11 PM -
I don't see why it would be, but it would be better to test it using the WACK.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.Tuesday, June 5, 2012 2:44 PMModerator -
About the Password vault, all is ok. (BTW, another app using the same resources may be able to retrieve the username/password? or the vault is app dépendent?
The Http Client sample don't show how to define credentials, I have setted the
HttpClient.DefaultRequestHeaders.Authorization
But I have found no helpers to create BasicAuth. I have created it manually with the following code:string basic = Username + ":" + Password; List<byte> arr = new List<byte>(); foreach (var c in basic) // ASCII encoding not present { arr.Add((byte)c); } var base64 = Convert.ToBase64String(arr.ToArray()); return new AuthenticationHeaderValue("Basic", base64);
Any better (more integrated) solution?
- Marked as answer by SirLeamont Wednesday, June 6, 2012 9:13 AM
Tuesday, June 5, 2012 3:51 PM -
Steps 1-4 explain how to define credentials and the auth type.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.Thursday, June 7, 2012 2:01 PMModerator