public void GetAppServiceIdentity_XMsClientPrincipalCorrectEasyAuthFormat_ReturnsEasyAuthIdentity() { HttpRequest req = new DefaultHttpContext().Request; var claims = new List <Claim>(); claims.Add(new Claim("name", "Connor McMahon")); claims.Add(new Claim("role", "Software Engineer")); var identity = new ClaimsIdentity(authenticationType: "aad", nameType: "name", roleType: "role", claims: claims); //Load onto header string json = JsonConvert.SerializeObject(ClaimsIdentitySlim.FromClaimsIdentity(identity)); byte[] bytes = Encoding.UTF8.GetBytes(json); string encodedHeaderValue = Convert.ToBase64String(bytes); req.Headers["x-ms-client-principal"] = encodedHeaderValue; ClaimsIdentity easyAuthIdentity = req.GetAppServiceIdentity(); Assert.NotNull(easyAuthIdentity); Assert.Equal("aad", easyAuthIdentity.AuthenticationType); Assert.Equal("name", easyAuthIdentity.NameClaimType); Assert.Equal("role", easyAuthIdentity.RoleClaimType); var claim1 = easyAuthIdentity.Claims.ElementAt(0); Assert.Equal("name", claim1.Type); Assert.Equal("Connor McMahon", claim1.Value); var claim2 = easyAuthIdentity.Claims.ElementAt(1); Assert.Equal("role", claim2.Type); Assert.Equal("Software Engineer", claim2.Value); }
public void GetAppServiceIdentity_XMsClientPrincipalInvalidJson_ReturnsNull() { HttpRequest req = new DefaultHttpContext().Request; var claims = new List <Claim>(); claims.Add(new Claim("name", "Connor McMahon")); claims.Add(new Claim("role", "Software Engineer")); var identity = new ClaimsIdentity(authenticationType: "aad", nameType: "name", roleType: "role", claims: claims); //Load onto header string json = JsonConvert.SerializeObject(ClaimsIdentitySlim.FromClaimsIdentity(identity)); json = json.Replace("[", "{"); json = json.Replace("]", "}"); byte[] bytes = Encoding.UTF8.GetBytes(json); string encodedHeaderValue = Convert.ToBase64String(bytes); req.Headers["x-ms-client-principal"] = encodedHeaderValue; Assert.Null(req.GetAppServiceIdentity()); }