yavsc/testOauthClient/Startup.cs

131 lines
5.3 KiB
C#
Raw Normal View History

2016-06-01 23:47:06 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.AspNet.Authentication.OpenIdConnect;
using Microsoft.AspNet.Authentication;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Authentication.Cookies;
2016-06-07 00:20:05 +02:00
using Yavsc.Auth;
using Microsoft.Extensions.WebEncoders;
2016-06-01 23:47:06 +02:00
namespace testOauthClient
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
2016-06-02 00:37:51 +02:00
services.Configure<SharedAuthenticationOptions>(options => {
2016-06-01 23:47:06 +02:00
options.SignInScheme = "ClientCookie";
});
2016-06-02 00:37:51 +02:00
2016-06-07 00:20:05 +02:00
services.AddTransient<Microsoft.Extensions.WebEncoders.UrlEncoder, UrlEncoder>();
2016-06-02 00:37:51 +02:00
services.AddAuthentication();
2016-06-01 23:47:06 +02:00
services.AddMvc();
2016-06-02 00:37:51 +02:00
2016-06-01 23:47:06 +02:00
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
2016-06-02 00:37:51 +02:00
app.UseIISPlatformHandler(options => {
options.AuthenticationDescriptions.Clear();
});
app.UseStaticFiles();
2016-06-07 00:20:05 +02:00
app.UseOAuthAuthentication(
options => { 
options.AuthenticationScheme="yavsc";
options.AuthorizationEndpoint="http://dev.pschneider.fr/signin";
options.TokenEndpoint="http://dev.pschneider.fr/token";
options.AutomaticAuthenticate=true;
options.AutomaticChallenge=true;
options.CallbackPath=new PathString("/signin-yavsc");
options.ClaimsIssuer="http://dev.pschneider.fr";
options.ClientId="016c5ae4-f4cd-40e3-b250-13701c871ecd";
options.ClientSecret="blahblah";
}
);
2016-06-01 23:47:06 +02:00
app.UseCookieAuthentication(new CookieAuthenticationOptions {
AutomaticAuthenticate = true,
AutomaticChallenge = true,
AuthenticationScheme = "ClientCookie",
CookieName = CookieAuthenticationDefaults.CookiePrefix + "ClientCookie",
ExpireTimeSpan = TimeSpan.FromMinutes(5),
LoginPath = new PathString("/signin"),
LogoutPath = new PathString("/signout")
});
2016-06-06 12:38:11 +02:00
2016-06-01 23:47:06 +02:00
2016-06-07 00:20:05 +02:00
/* app.UseOpenIdConnectAuthentication(
2016-06-06 12:38:11 +02:00
options => {
options.AuthenticationScheme = OpenIdConnectDefaults.AuthenticationScheme;
options.RequireHttpsMetadata = false;
2016-06-01 23:47:06 +02:00
// Note: these settings must match the application details
// inserted in the database at the server level.
2016-06-06 12:38:11 +02:00
options.ClientId = "016c5ae4-f4cd-40e3-b250-13701c871ecd";
options.ClientSecret = "blahblah";
options.PostLogoutRedirectUri = "http://dev.pschneider.fr/";
2016-06-01 23:47:06 +02:00
// Use the authorization code flow.
2016-06-06 12:38:11 +02:00
options.ResponseType = OpenIdConnectResponseTypes.Code;
2016-06-01 23:47:06 +02:00
// Note: setting the Authority allows the OIDC client middleware to automatically
// retrieve the identity provider's configuration and spare you from setting
// the different endpoints URIs or the token validation parameters explicitly.
2016-06-06 12:38:11 +02:00
options.Authority = "http://dev.pschneider.fr/";
// Note: the resource property represents the different endpoints the
// access token should be issued for (values must be space-delimited).
options.Resource = "http://dev.pschneider.fr/";
2016-06-06 17:13:39 +02:00
options.Scope.Clear();
options.Scope.Add("openid");
// .Add("api-resource-controller");
2016-06-07 00:20:05 +02:00
}); */
2016-06-01 23:47:06 +02:00
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
// Entry point for the application.
public static void Main(string[] args) => Microsoft.AspNet.Hosting.WebApplication.Run<Startup>(args);
}
}