94 lines
3.6 KiB
C#
94 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Identity.ExternalClaims.Data;
|
|
using Identity.ExternalClaims.Services;
|
|
using System.Security.Claims;
|
|
|
|
namespace Identity.ExternalClaims
|
|
{
|
|
public class Startup
|
|
{
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddDbContext<ApplicationDbContext>(options =>
|
|
options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=aspnet-Identity.ExternalClaims-53bc9b9d-9d6a-45d4-8429-2a2761773502;Trusted_Connection=True;MultipleActiveResultSets=true"));
|
|
|
|
services.AddIdentity<ApplicationUser, IdentityRole>()
|
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
|
.AddDefaultTokenProviders();
|
|
|
|
services.AddAuthentication().AddGoogle(o =>
|
|
{
|
|
// Configure your auth keys, usually stored in Config or User Secrets
|
|
o.ClientId = "<yourid>";
|
|
o.ClientSecret = "<yoursecret>";
|
|
o.Scope.Add("https://www.googleapis.com/auth/plus.login");
|
|
o.ClaimActions.MapJsonKey(ClaimTypes.Gender, "gender");
|
|
o.SaveTokens = true;
|
|
o.Events.OnCreatingTicket = ctx =>
|
|
{
|
|
List<AuthenticationToken> tokens = ctx.Properties.GetTokens() as List<AuthenticationToken>;
|
|
tokens.Add(new AuthenticationToken() { Name = "TicketCreated", Value = DateTime.UtcNow.ToString() });
|
|
ctx.Properties.StoreTokens(tokens);
|
|
return Task.CompletedTask;
|
|
};
|
|
});
|
|
|
|
services.AddMvc()
|
|
.AddRazorPagesOptions(options =>
|
|
{
|
|
options.Conventions.AuthorizeFolder("/Account/Manage");
|
|
options.Conventions.AuthorizePage("/Account/Logout");
|
|
options.Conventions.AuthorizePage("/MyClaims");
|
|
});
|
|
|
|
// Register no-op EmailSender used by account confirmation and password reset during development
|
|
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=532713
|
|
services.AddSingleton<IEmailSender, EmailSender>();
|
|
}
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
|
{
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
app.UseDatabaseErrorPage();
|
|
}
|
|
else
|
|
{
|
|
app.UseExceptionHandler("/Error");
|
|
}
|
|
|
|
app.UseStaticFiles();
|
|
|
|
app.UseAuthentication();
|
|
|
|
app.UseMvc(routes =>
|
|
{
|
|
routes.MapRoute(
|
|
name: "default",
|
|
template: "{controller}/{action=Index}/{id?}");
|
|
});
|
|
}
|
|
}
|
|
}
|