【问题标题】:ASP.NET MVC Application Identity does not share with ASP.NET Core 2ASP.NET MVC 应用程序标识不与 ASP.NET Core 2 共享
【发布时间】:2017-08-25 11:24:43
【问题描述】:

我有一个 ASP.Net Core 2 项目,我设置了所有身份验证配置

在 ASP.Net Core 2 Preview2\Start.cs 中

var protectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"C:\SharedFolder"));
var dataProtector = protectionProvider.CreateProtector(
                                    "CookieAuthenticationMiddleware",
                                    "Cookie",
                                    "v2");
var ticketFormat = new TicketDataFormat(dataProtector);
services.AddIdentity<ApplicationUser, IdentityRole>()
                            .AddEntityFrameworkStores<ApplicationDbContext>()
                            .AddDefaultTokenProviders();
services.AddAuthentication(o =>{
                        o.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                        o.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                        o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;                    
                    });
services.ConfigureApplicationCookie(c => {
                    c.CookieName = "myapplication";               
                    c.ExpireTimeSpan =  TimeSpan.FromDays(3);
                    c.CookieSecure = CookieSecurePolicy.None;
                    c.SlidingExpiration = true;
                });

在ASP.Net MVC4\ConfigureAuth函数中:

var protectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"C:\SharedFolder"));
var dataProtector = protectionProvider.CreateProtector(
                    "CookieAuthenticationMiddleware",
                    "Cookie",
                    "v2");
var ticketFormat = new AspNetTicketDataFormat(new DataProtectorShim(dataProtector));
app.UseCookieAuthentication(new CookieAuthenticationOptions{
                AuthenticationType = "Cookie",
                AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
                CookieName = "myapplication",
                CookieDomain = "localhost",
                TicketDataFormat = ticketFormat,
                CookieManager = new ChunkingCookieManager()
            });

share Cookies 工作正常我在 Asp.Net Core2 项目中设置了 cookie,我从 MVC 项目中读取它,反之亦然,当我检查用户 IsAuthenticated 时,在 MVC 项目中总是得到错误。

如何在 MVC 项目中获取用户身份?任何帮助都非常受欢迎,在此先感谢。

【问题讨论】:

    标签: asp.net asp.net-mvc-4 cookies .net-core


    【解决方案1】:

    我通过在 ASP.Net Core 2 Preview2\Start.cs 中提交几行代码解决了我的问题

    /*services.AddAuthentication(o =>{
                            o.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                            o.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                            o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;                    
                        });*/
    

    我将 ConfigureApplicationCookie 更改为

     services.ConfigureApplicationCookie(c =>
                {
                    c.CookieName = "myapplication";
                    c.ExpireTimeSpan = TimeSpan.FromDays(3);
                    c.SlidingExpiration = true;
                    c.CookieDomain = "localhost";
                    c.TicketDataFormat = ticketFormat;
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      • 2018-12-20
      • 2018-12-24
      • 1970-01-01
      • 2022-01-06
      • 2018-11-30
      • 1970-01-01
      相关资源
      最近更新 更多