Skip to content

Instantly share code, notes, and snippets.

View jpda's full-sized avatar
🤔
ok

John Patrick Dandison jpda

🤔
ok
View GitHub Profile
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- snip -->
<bearerTokenRequired/>
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
internal class WcfErrorResponseData
{
public WcfErrorResponseData(HttpStatusCode status) : this(status, string.Empty, new KeyValuePair<string, string>[0]){ }
public WcfErrorResponseData(HttpStatusCode status, string body) : this(status, body, new KeyValuePair<string, string>[0]) { }
public WcfErrorResponseData(HttpStatusCode status, string body, params KeyValuePair<string, string>[] headers)
{
StatusCode = status;
Body = body;
Headers = headers;
@jpda
jpda / AuthorizationHeaderMessageInspector.cs
Created January 12, 2015 21:27
AuthorizationHeaderMessageInspector.cs
public class AuthorizationHeaderMessageInspector : IClientMessageInspector
{
private readonly string _token;
public AuthorizationHeaderMessageInspector(string token)
{
_token = string.IsNullOrEmpty(token) ? AzureAdToken.Get() : token;
}
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
@jpda
jpda / AzureAdToken.cs
Created January 12, 2015 21:47
AzureAdToken.cs
public static class AzureAdToken
{
public static string Get()
{
const string clientSecret = "CLIENT_SECRET";
const string clientId = "CLIENT_ID";
var ctx = new AuthenticationContext("https://login.windows.net/TENANT_NAME_OR_ID");
var token = ctx.AcquireToken("TARGET_RESOURCE", new ClientCredential(clientId, clientSecret));
return token.AccessToken;
}
@jpda
jpda / AuthorizationHeaderEndpointBehavior.cs
Created January 12, 2015 21:52
AuthorizationHeaderEndpointBehavior.cs
public class AuthorizationHeaderEndpointBehavior : IEndpointBehavior
{
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(new AuthorizationHeaderMessageInspector(AzureAdToken.Get()));
}
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
@jpda
jpda / EndpointExtension.cs
Created January 12, 2015 21:53
EndpointExtension.cs
public static class EndpointExtension
{
public static void AddAuthorizationEndpointBehavior(this ServiceEndpoint endpoint)
{
endpoint.EndpointBehaviors.Add(new AuthorizationHeaderEndpointBehavior());
}
}
var a = new Auth.AuthServiceClient();
a.Endpoint.AddAuthorizationEndpointBehavior();
var me = a.WhoAmI();
Console.WriteLine(me);
Console.ReadLine();
@jpda
jpda / manifest.json
Created January 12, 2015 22:07
manifest.json
"oauth2Permissions": [
{
"adminConsentDescription": "Allow the application access to the service",
"adminConsentDisplayName": "Have full access to the service",
"id": "b69ee3c9-c40d-4f2a-ac80-961cd1534e40",
"isEnabled": true,
"origin": "Application",
"type": "User",
"userConsentDescription": "Allow the application full access to the service on your behalf",
"userConsentDisplayName": "Have full access to the service",
@jpda
jpda / Silly.cs
Last active August 29, 2015 14:18
Attribute Metadata Silliness
public async static void AddDocuments<T>(IList<T> things) where T : class, new()
{
var indexClient = new SearchIndexClient(ConfigurationManager.AppSettings["SearchServiceName"], things.First().GetType().Name.ToLower(), new SearchCredentials(ConfigurationManager.AppSettings["SearchServiceApiKey"]));
try
{
Trace.WriteLine($"Adding {things.Count()} documents to the index...");
var thingsToIndex = things.Where(x => x.GetType().GetProperties().Any(prop => prop.GetCustomAttribute<IndexMetadataAttribute>() != null));
var newThings = thingsToIndex.Select(x =>
{
@jpda
jpda / SomeEntity.cs
Created April 8, 2015 05:33
Attribute Metadata
public class SomeEntity : TableEntity
{
[IndexMetadata("Id", IsKey = true, IsRetrievable = true, IsSearchable = true, IsSortable = true)]
public string Id
{
get { return RowKey; }
set { RowKey = value; }
}
[IndexMetadata("Title", IsKey = false, IsRetrievable = true, IsSearchable = true, IsSortable = true)]
public string Title { get; set; }