【问题标题】:Asp.net Core 2.0 basic JWT missing ApplicationCookieAsp.net Core 2.0 基本 JWT 缺少 ApplicationCookie
【发布时间】:2018-01-24 09:33:53
【问题描述】:

我目前正在将我的 Web API 项目升级到 .NET Core 2.0 并且似乎 AddIdentity.Cookies 不再可用。

谁能指出我正确的方向?

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton(Configuration);

        services.AddMvc();

        // Add framework services.
        services.AddDbContext<ApplicationContext>(
            options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddDbContext<AuthContext>(options => options.UseSqlServer(Configuration.GetConnectionString("AuthConntection")));

        services.AddIdentity<ApplicationUser, MyRole>(cfg =>
        {
            cfg.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents
            {
                OnRedirectToLogin = ctx =>
                {
                    if (ctx.Request.Path.StartsWithSegments("/api"))
                        ctx.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized;
                    return Task.FromResult(0);
                }
            };
        })
            .AddUserStore<ApplicationUserStore>()
            .AddDefaultTokenProviders()
            .AddEntityFrameworkStores<AuthContext, Guid>();}

【问题讨论】:

    标签: asp.net-core jwt asp.net-core-2.0


    【解决方案1】:

    根据 GitHub 上的代码,我觉得你应该使用ConfigureApplicationCookie

    services.AddIdentity<ApplicationUser, MyRole>();
    services.ConfigureApplicationCookie(options =>
    {
        options.Events = new CookieAuthenticationEvents
        {
        };
    });
    

    GitHub上的扩展方法:https://github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNetCore.Identity/IdentityServiceCollectionExtensions.cs#L39

    【讨论】:

      猜你喜欢
      • 2018-01-24
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多