Last active
December 30, 2015 14:43
-
-
Save sshibani/85d1407ca5dcba595629 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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