This is a Web Performance test plugin that get's the values from 2 context parameters and encrypts them in base64, before running the call:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.VisualStudio.TestTools.WebTesting;
namespace Base64 { public class Encrypt64 : WebTestPlugin { [System.ComponentModel.DisplayName("Context Parameter Name")] [System.ComponentModel.Description("Name of the webtest context parameter")] public string ContextParameterName { get; set; }
// encode to Base64 function static public string EncodeTo64(string toEncode) { byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode); string returnValue = System.Convert.ToBase64String(toEncodeAsBytes); return returnValue; }
// decode from Base64 function static public string DecodeFrom64(string encodedData) { byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData); string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes); return returnValue; }
// this will be executed before the call public override void PreWebTest(object sender, PreWebTestEventArgs e) { string decode64; decode64 = EncodeTo64(e.WebTest.Context["email"] + ":" + e.WebTest.Context["pass"]); e.WebTest.Context["GetBase64"] = decode64; }
// this will be executed after the call public override void PostWebTest(object sender, PostWebTestEventArgs e) { } } }
Scenario:
1. In a Visual Studio Web Performance test, make call to a web service that requires credentials. => you will get 401 - Unauthorized error (if you did not specify credentials) Solution: 1. Add a Header (for Basic authentication) to the web test request: Right-click on the call -> Add Header. 2. Add the following info: Name: "Authorization" Value: “Basic ” + encodeBase64 (“username:password”)
Use http://www.base64encode.org/ to encode username and password in the following format: "username:password". It should look like this: