Created
May 17, 2018 02:15
-
-
Save ichiroku11/c0a988b70fea6e1be20407436d420fcb to your computer and use it in GitHub Desktop.
グローバルフィルタで承認(認証というかアクセス制御というか)
This file contains hidden or 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.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Authentication.Cookies; | |
using Microsoft.AspNetCore.Authorization; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Mvc.Authorization; | |
using Microsoft.AspNetCore.Routing; | |
using Microsoft.Extensions.DependencyInjection; | |
namespace WebApp { | |
public class Startup { | |
public void ConfigureServices(IServiceCollection services) { | |
// クッキー認証を行うためにサービスを登録 | |
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) | |
.AddCookie(); | |
services.AddMvc(options => { | |
var policy = new AuthorizationPolicyBuilder() | |
.RequireAuthenticatedUser() | |
.Build(); | |
options.Filters.Add(new AuthorizeFilter(policy)); | |
}); | |
services.Configure<RouteOptions>(options => { | |
options.LowercaseUrls = true; | |
}); | |
} | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { | |
if (env.IsDevelopment()) { | |
app.UseDeveloperExceptionPage(); | |
} | |
app.UseMvc(routes => { | |
routes.MapRoute( | |
name: "default", | |
template: "{controller=Default}/{action=Index}/{id?}"); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment