Skip to content

Instantly share code, notes, and snippets.

@sshibani
Last active December 30, 2015 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sshibani/85d1407ca5dcba595629 to your computer and use it in GitHub Desktop.
Save sshibani/85d1407ca5dcba595629 to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using DD4T.RestService.WebApi;
using Autofac;
using Autofac.Integration.WebApi;
using DD4T.ContentModel.Contracts.Logging;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security;
using Thinktecture.IdentityModel.Owin;
using System.Collections.Generic;
using System.Security.Claims;
using System.Configuration;
[assembly: OwinStartup(typeof(MyDD4T.RestService.Startup))]
namespace MyDD4T.RestService
{
public class Startup
{
private const string authenticationType = "Basic";
public void Configuration(IAppBuilder app)
{
log4net.Config.XmlConfigurator.Configure();
app.UseBasicAuthentication(new BasicAuthenticationOptions("MyDD4T login", Validate)
{
AuthenticationMode = AuthenticationMode.Active,
AuthenticationType = authenticationType
});
app.ForceAuthentication(authenticationType);
app.UseDD4TWebApi();
}
private Task<IEnumerable<Claim>> Validate(string id, string secret)
{
var username = ConfigurationManager.AppSettings["Username"];
var password = ConfigurationManager.AppSettings["Password"];
List<Claim> list = null;
if (id.Equals(username) && secret.Equals(password))
{
list = new List<Claim>();
list.Add(new Claim(ClaimTypes.Name, id));
}
return Task.FromResult<IEnumerable<Claim>>(list);
}
}
}
//// Extensions methods
namespace Owin
{
public static class AppBuilderExtensions
{
public static IAppBuilder ForceAuthentication(this IAppBuilder app, string authenticationType)
{
return app.Use(async (ctx, next) =>
{
var result = await ctx.Authentication.AuthenticateAsync(authenticationType);
if (result == null || result.Identity == null)
{
ctx.Authentication.Challenge(authenticationType);
}
else
{
await next();
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment