如何在ASP.net核心1.1中使用CookieAuthentication

时间:2017-07-02 04:07:12

标签: c# asp.net-mvc authentication cookies asp.net-core

我是ASP.net核心的新手,我设法使用Cookie Authentication而不是我以前的会话身份验证方式。我已经按照上面的教程中的步骤阅读了How to use CookieAuthentication in dotnet core mvc properly? 但我可以&# 39;在我的应用程序中找到使用此身份验证的方法。任何简单的示例都会对我有所帮助 的 Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationScheme = "MyScheme",
        LoginPath = new Microsoft.AspNetCore.Http.PathString("/Account/Login"),
        AccessDeniedPath = new Microsoft.AspNetCore.Http.PathString("/Account/Forbidden"),
        AutomaticAuthenticate = true,
        AutomaticChallenge = true,
    });
    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}
...
public void ConfigureServices(IServiceCollection services)
{
    services.AddApplicationInsightsTelemetry(Configuration);

    // Add framework services.
    services.AddMvc();
    services.AddAuthentication();
    services.AddAuthorization();
}

的AccountController

public class AccountController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    public String Login()
    {
        return "Please login";
    }

    public String Forbidden()
    {
        return "Forbidden";
    }

}

0 个答案:

没有答案