ASP.NET Core 2.1 Unable to share cookies among applications










1















I am trying to share cookies among applications so that I don't have to constantly login on one app and login again in another.



This is the startup that I have on both applications



services.AddDbContext<DbContext>(options => options.UseSqlServer(Configuration["DatabaseConfiguration:ConnectionString"]));
services.AddMvc().AddJsonOptions(options =>

options.SerializerSettings.ContractResolver
= new Newtonsoft.Json.Serialization.DefaultContractResolver();
);
services.AddIdentity<ApplicationUsers, Roles>()
.AddEntityFrameworkStores<DbContext>()
.AddDefaultTokenProviders();
services.AddScoped<LogsData>();
services.AddScoped<ApplicationUsersData>();
services.AddScoped<CustomClaimsCookieSignInHelper<ApplicationUsers>>();

services.Configure<IdentityOptions>(options =>

options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 6;
options.User.AllowedUserNameCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
options.User.RequireUniqueEmail = false;
options.SignIn.RequireConfirmedEmail = false;
options.SignIn.RequireConfirmedPhoneNumber = false;
);

services.Configure<CookiePolicyOptions>(options =>

// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
);
services.AddDataProtection()
.PersistKeysToFileSystem(KeyRingConfigurationManager.GetBasePath())
.SetApplicationName("AppName");

services.ConfigureApplicationCookie(options =>
options.Cookie.Name = ".AppName.Shared";
);

services.ConfigureApplicationCookie(options =>

// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);

options.LoginPath = "/Identity/Account/Login";
options.AccessDeniedPath = "/Account/AccessDenied";
options.SlidingExpiration = true;
);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);


When I run my application, I noticed that in my Key folder, a key xml file appears. However, when I start the second application and navigate to my controller that is authorized, I get kicked back to the login page meaning I wasn't logged in. What else am I missing?










share|improve this question
























  • Do you have two different applications?

    – Ajar
    Nov 16 '18 at 5:24











  • Yes but application 2 has references to certain components within application 1

    – JianYA
    Nov 16 '18 at 5:38











  • Okay. i got some ideas from the below msdn site. it may helps you, refer link

    – Ajar
    Nov 16 '18 at 5:49






  • 1





    Thats what I was following.

    – JianYA
    Nov 16 '18 at 5:50















1















I am trying to share cookies among applications so that I don't have to constantly login on one app and login again in another.



This is the startup that I have on both applications



services.AddDbContext<DbContext>(options => options.UseSqlServer(Configuration["DatabaseConfiguration:ConnectionString"]));
services.AddMvc().AddJsonOptions(options =>

options.SerializerSettings.ContractResolver
= new Newtonsoft.Json.Serialization.DefaultContractResolver();
);
services.AddIdentity<ApplicationUsers, Roles>()
.AddEntityFrameworkStores<DbContext>()
.AddDefaultTokenProviders();
services.AddScoped<LogsData>();
services.AddScoped<ApplicationUsersData>();
services.AddScoped<CustomClaimsCookieSignInHelper<ApplicationUsers>>();

services.Configure<IdentityOptions>(options =>

options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 6;
options.User.AllowedUserNameCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
options.User.RequireUniqueEmail = false;
options.SignIn.RequireConfirmedEmail = false;
options.SignIn.RequireConfirmedPhoneNumber = false;
);

services.Configure<CookiePolicyOptions>(options =>

// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
);
services.AddDataProtection()
.PersistKeysToFileSystem(KeyRingConfigurationManager.GetBasePath())
.SetApplicationName("AppName");

services.ConfigureApplicationCookie(options =>
options.Cookie.Name = ".AppName.Shared";
);

services.ConfigureApplicationCookie(options =>

// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);

options.LoginPath = "/Identity/Account/Login";
options.AccessDeniedPath = "/Account/AccessDenied";
options.SlidingExpiration = true;
);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);


When I run my application, I noticed that in my Key folder, a key xml file appears. However, when I start the second application and navigate to my controller that is authorized, I get kicked back to the login page meaning I wasn't logged in. What else am I missing?










share|improve this question
























  • Do you have two different applications?

    – Ajar
    Nov 16 '18 at 5:24











  • Yes but application 2 has references to certain components within application 1

    – JianYA
    Nov 16 '18 at 5:38











  • Okay. i got some ideas from the below msdn site. it may helps you, refer link

    – Ajar
    Nov 16 '18 at 5:49






  • 1





    Thats what I was following.

    – JianYA
    Nov 16 '18 at 5:50













1












1








1








I am trying to share cookies among applications so that I don't have to constantly login on one app and login again in another.



This is the startup that I have on both applications



services.AddDbContext<DbContext>(options => options.UseSqlServer(Configuration["DatabaseConfiguration:ConnectionString"]));
services.AddMvc().AddJsonOptions(options =>

options.SerializerSettings.ContractResolver
= new Newtonsoft.Json.Serialization.DefaultContractResolver();
);
services.AddIdentity<ApplicationUsers, Roles>()
.AddEntityFrameworkStores<DbContext>()
.AddDefaultTokenProviders();
services.AddScoped<LogsData>();
services.AddScoped<ApplicationUsersData>();
services.AddScoped<CustomClaimsCookieSignInHelper<ApplicationUsers>>();

services.Configure<IdentityOptions>(options =>

options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 6;
options.User.AllowedUserNameCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
options.User.RequireUniqueEmail = false;
options.SignIn.RequireConfirmedEmail = false;
options.SignIn.RequireConfirmedPhoneNumber = false;
);

services.Configure<CookiePolicyOptions>(options =>

// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
);
services.AddDataProtection()
.PersistKeysToFileSystem(KeyRingConfigurationManager.GetBasePath())
.SetApplicationName("AppName");

services.ConfigureApplicationCookie(options =>
options.Cookie.Name = ".AppName.Shared";
);

services.ConfigureApplicationCookie(options =>

// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);

options.LoginPath = "/Identity/Account/Login";
options.AccessDeniedPath = "/Account/AccessDenied";
options.SlidingExpiration = true;
);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);


When I run my application, I noticed that in my Key folder, a key xml file appears. However, when I start the second application and navigate to my controller that is authorized, I get kicked back to the login page meaning I wasn't logged in. What else am I missing?










share|improve this question
















I am trying to share cookies among applications so that I don't have to constantly login on one app and login again in another.



This is the startup that I have on both applications



services.AddDbContext<DbContext>(options => options.UseSqlServer(Configuration["DatabaseConfiguration:ConnectionString"]));
services.AddMvc().AddJsonOptions(options =>

options.SerializerSettings.ContractResolver
= new Newtonsoft.Json.Serialization.DefaultContractResolver();
);
services.AddIdentity<ApplicationUsers, Roles>()
.AddEntityFrameworkStores<DbContext>()
.AddDefaultTokenProviders();
services.AddScoped<LogsData>();
services.AddScoped<ApplicationUsersData>();
services.AddScoped<CustomClaimsCookieSignInHelper<ApplicationUsers>>();

services.Configure<IdentityOptions>(options =>

options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 6;
options.User.AllowedUserNameCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
options.User.RequireUniqueEmail = false;
options.SignIn.RequireConfirmedEmail = false;
options.SignIn.RequireConfirmedPhoneNumber = false;
);

services.Configure<CookiePolicyOptions>(options =>

// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
);
services.AddDataProtection()
.PersistKeysToFileSystem(KeyRingConfigurationManager.GetBasePath())
.SetApplicationName("AppName");

services.ConfigureApplicationCookie(options =>
options.Cookie.Name = ".AppName.Shared";
);

services.ConfigureApplicationCookie(options =>

// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);

options.LoginPath = "/Identity/Account/Login";
options.AccessDeniedPath = "/Account/AccessDenied";
options.SlidingExpiration = true;
);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);


When I run my application, I noticed that in my Key folder, a key xml file appears. However, when I start the second application and navigate to my controller that is authorized, I get kicked back to the login page meaning I wasn't logged in. What else am I missing?







asp.net asp.net-mvc asp.net-core asp.net-core-mvc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 4:30







JianYA

















asked Nov 16 '18 at 4:12









JianYAJianYA

5431130




5431130












  • Do you have two different applications?

    – Ajar
    Nov 16 '18 at 5:24











  • Yes but application 2 has references to certain components within application 1

    – JianYA
    Nov 16 '18 at 5:38











  • Okay. i got some ideas from the below msdn site. it may helps you, refer link

    – Ajar
    Nov 16 '18 at 5:49






  • 1





    Thats what I was following.

    – JianYA
    Nov 16 '18 at 5:50

















  • Do you have two different applications?

    – Ajar
    Nov 16 '18 at 5:24











  • Yes but application 2 has references to certain components within application 1

    – JianYA
    Nov 16 '18 at 5:38











  • Okay. i got some ideas from the below msdn site. it may helps you, refer link

    – Ajar
    Nov 16 '18 at 5:49






  • 1





    Thats what I was following.

    – JianYA
    Nov 16 '18 at 5:50
















Do you have two different applications?

– Ajar
Nov 16 '18 at 5:24





Do you have two different applications?

– Ajar
Nov 16 '18 at 5:24













Yes but application 2 has references to certain components within application 1

– JianYA
Nov 16 '18 at 5:38





Yes but application 2 has references to certain components within application 1

– JianYA
Nov 16 '18 at 5:38













Okay. i got some ideas from the below msdn site. it may helps you, refer link

– Ajar
Nov 16 '18 at 5:49





Okay. i got some ideas from the below msdn site. it may helps you, refer link

– Ajar
Nov 16 '18 at 5:49




1




1





Thats what I was following.

– JianYA
Nov 16 '18 at 5:50





Thats what I was following.

– JianYA
Nov 16 '18 at 5:50












1 Answer
1






active

oldest

votes


















0














can you please check about adding session with your services? I have tried some code regarding that and it does working fine for me.



 services.AddSession(options =>

// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromDays(2);
options.Cookie.HttpOnly = true;
options.Cookie.Name = CookieAuthenticationDefaults.AuthenticationScheme;
);


Also add usesession() in your COnfigure() method.



 app.UseSession();





share|improve this answer























  • Must I use sessions for this?

    – JianYA
    Nov 16 '18 at 5:18











  • As i know, session helps to save cookies for the application. Once session expired your cookies does not works.

    – Ajar
    Nov 16 '18 at 5:23










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331333%2fasp-net-core-2-1-unable-to-share-cookies-among-applications%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














can you please check about adding session with your services? I have tried some code regarding that and it does working fine for me.



 services.AddSession(options =>

// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromDays(2);
options.Cookie.HttpOnly = true;
options.Cookie.Name = CookieAuthenticationDefaults.AuthenticationScheme;
);


Also add usesession() in your COnfigure() method.



 app.UseSession();





share|improve this answer























  • Must I use sessions for this?

    – JianYA
    Nov 16 '18 at 5:18











  • As i know, session helps to save cookies for the application. Once session expired your cookies does not works.

    – Ajar
    Nov 16 '18 at 5:23















0














can you please check about adding session with your services? I have tried some code regarding that and it does working fine for me.



 services.AddSession(options =>

// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromDays(2);
options.Cookie.HttpOnly = true;
options.Cookie.Name = CookieAuthenticationDefaults.AuthenticationScheme;
);


Also add usesession() in your COnfigure() method.



 app.UseSession();





share|improve this answer























  • Must I use sessions for this?

    – JianYA
    Nov 16 '18 at 5:18











  • As i know, session helps to save cookies for the application. Once session expired your cookies does not works.

    – Ajar
    Nov 16 '18 at 5:23













0












0








0







can you please check about adding session with your services? I have tried some code regarding that and it does working fine for me.



 services.AddSession(options =>

// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromDays(2);
options.Cookie.HttpOnly = true;
options.Cookie.Name = CookieAuthenticationDefaults.AuthenticationScheme;
);


Also add usesession() in your COnfigure() method.



 app.UseSession();





share|improve this answer













can you please check about adding session with your services? I have tried some code regarding that and it does working fine for me.



 services.AddSession(options =>

// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromDays(2);
options.Cookie.HttpOnly = true;
options.Cookie.Name = CookieAuthenticationDefaults.AuthenticationScheme;
);


Also add usesession() in your COnfigure() method.



 app.UseSession();






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 5:16









AjarAjar

948




948












  • Must I use sessions for this?

    – JianYA
    Nov 16 '18 at 5:18











  • As i know, session helps to save cookies for the application. Once session expired your cookies does not works.

    – Ajar
    Nov 16 '18 at 5:23

















  • Must I use sessions for this?

    – JianYA
    Nov 16 '18 at 5:18











  • As i know, session helps to save cookies for the application. Once session expired your cookies does not works.

    – Ajar
    Nov 16 '18 at 5:23
















Must I use sessions for this?

– JianYA
Nov 16 '18 at 5:18





Must I use sessions for this?

– JianYA
Nov 16 '18 at 5:18













As i know, session helps to save cookies for the application. Once session expired your cookies does not works.

– Ajar
Nov 16 '18 at 5:23





As i know, session helps to save cookies for the application. Once session expired your cookies does not works.

– Ajar
Nov 16 '18 at 5:23



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331333%2fasp-net-core-2-1-unable-to-share-cookies-among-applications%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線