简体   繁体   中英

Sharing cookie between two asp.net mvc applications

I have following problem: I did research about sharing cookie between two applications on a same domain but I'm pretty sure I'm missing something and didn't found my answer yet.

I have two apps on webistes like this: app.domain.com and portal.app.domain.com First application is Orchard cms, I created my own custom membership provider so authentication in orchard looks like this:

 if (ModelState.IsValid && CustomMembershipProvider.ValidateUser(userNameOrEmail, password))
 {
    FormsAuthentication.SetAuthCookie(userNameOrEmail, true);
 }

and web.config in both apps.

<authentication mode="Forms">
      <forms name="loginCookie" path="/" loginUrl="~/Users/Account/AccessDenied" timeout="2880" cookieless="UseCookies"
         enableCrossAppRedirects ="true" domain=".domain.com"
         requireSSL="false"/>
    </authentication>

<machineKey validationKey="CB6DEDC99140ABCEA1CFE08BEF8407BE2AE60DDB905264F6F7FAA9650B5AB4F1986F84878B4842FB9B51EDD477FFC518C6B6F1687FF25D5BAE883C313F05A6A4" decryptionKey="B0F735974F5261E63D07F4571FB11B392477173098F71E7CA76C1DD9A459C638" validation="SHA1" decryption="AES" />

Second app is asp.net mvc 4 application.

Cookie is created every time so it works but portal.app.domain.com doesn't "see" loginCookie. Request.IsAuthenticated returns false and Request.Count doesn't contain valid number of cookies that I want. What's more when I sign on in portal.app.domain.com loginCookie is created in the exact same way but with new value. What is weird in I can fiddler see all cookies for domain. I found something about redirecting but can somone explain to me something more about it(what should I add? ), furthermore is it possibility to share cookie without redirect, I mean when sign on in app.domain.com user can write address portal.app.domain.com in browser and he can be logged in anyway?

Thank you in advance for all replies.

If i am not wrong there will be a default connection string by default in each of your project It will be as connectionStringName="DefaultConnection"

You will find it in web.config as coding take in my project i have

<membership defaultProvider="DefaultMembershipProvider">
  <providers>
    <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
  </providers>
</membership>

Set the DefaultConnection's connection string common in both your projects so that you can have the same being set for both of your projects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM