Я пытаюсь разделить cookie для аутентификации между различными приложениями в .net core 2.2.
Ниже приведен код из приложения 1 (comportocertlogin.local) startup.cs:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(@"C:\SVN\RS.3C\trunk\SourceCode\ComportoAdmin\ComportoAdmin.CertificateLogin"))
.SetApplicationName("SharedCookieApp");
//services.ConfigureApplicationCookie(options =>
//{
// options.Cookie.Name = ".AspNet.SharedCookie";
// options.Cookie.Domain = ".local";
//});
services.AddAuthentication(options =>
{
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(options =>
{
options.LoginPath = "/Login";
options.LogoutPath = "/Login";
options.Cookie.Name = ".AspNet.SharedCookie";
options.Cookie.Domain = ".local";
options.Cookie.Path = "/";
options.DataProtectionProvider =
DataProtectionProvider.Create(new DirectoryInfo(@"C:\SVN\RS.3C\trunk\SourceCode\ComportoAdmin\ComportoAdmin.CertificateLogin"));
});
Затем в приложении 1 ниже приведен код для создания файла cookie для проверки подлинности и перенаправления в приложение 2 * 1006.*
public async Task<IActionResult> OnPostAsync(int userId)
{
if (ModelState.IsValid)
{
//bool isValid = userId == 2; // TODO Validate the username and the password with your own logic
//if (!isValid)
//{
// ModelState.AddModelError("", "username or password is invalid");
// return Page();
//}
// Create the identity from the user info
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userId.ToString()));
identity.AddClaim(new Claim(ClaimTypes.Name, userId.ToString()));
identity.AddClaim(new Claim("UserId", userId.ToString()));
// Authenticate using the identity
var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { IsPersistent = false });
return Redirect("https://scomportoadmin.local/searchUserAccount");
}
return Page();
}
В приложении 2 (scomportoadmin.local) startup.cs У меня есть следующий код:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(@"C:\SVN\RS.3C\trunk\SourceCode\ComportoAdmin\ComportoAdmin.CertificateLogin"))
.SetApplicationName("SharedCookieApp");
//services.ConfigureApplicationCookie(options =>
//{
// options.Cookie.Name = ".AspNet.SharedCookie";
// options.Cookie.Domain = ".local";
//});
services.AddAuthentication(options =>
{
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(options =>
{
options.LoginPath = "/login";
options.LogoutPath = "/login";
options.Cookie.Name = ".AspNet.SharedCookie";
options.Cookie.Domain = ".local";
options.Cookie.Path = "/";
options.DataProtectionProvider =
DataProtectionProvider.Create(new DirectoryInfo(@"C:\SVN\RS.3C\trunk\SourceCode\ComportoAdmin\ComportoAdmin.CertificateLogin"));
});
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizePage("/SearchUserAccount");
options.Conventions.AuthorizePage("/EditCreateUserAccount");
options.Conventions.AllowAnonymousToPage("/RegisterUserAccount");
}).
SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
Чего-то не хватает, потому что я не могу получить доступ к страницам SearchUserAccount и EditCreateUserAccount в приложении 2. Чтоя здесь не так делаю?