4

On my Sitecore instance I'm authenticating a user programatically like this(adapted for this post):

[HttpGet]
        public ActionResult MyAction(MyModel model)
        {
             //custom logic
             Sitecore.Security.Authentication.AuthenticationManager.Login("extranet\\"+username,password);
        }

This works well. When I call the Sitecore.Context.User.IsAuthenticated property to check whether the user was authenticated it returns true. But that lasts until the ActionResult finishes it's execution. When I make a post or redirect the user to another page the IsAuthenticated property always returns false.

For example I have a post which gets executed on the same page declared as:

        [HttpPost]
        public ActionResult MyAction(MyModel model, string anotherParameter)
        { 
               //This always returns false
               if (Sitecore.Context.User.IsAuthenticated)
               {
                    //my custom logic. 
               }

        }

This is what my session state looks on the web.config

<sessionState mode="InProc" cookieless="false" timeout="20" sessionIDManagerType="Sitecore.SessionManagement.ConditionalSessionIdManager">
  <providers>
    <add name="mongo" type="Sitecore.SessionProvider.MongoDB.MongoSessionStateProvider, Sitecore.SessionProvider.MongoDB" sessionType="Standard" connectionStringName="session" pollingInterval="2" compression="true" />
    <add name="mssql" type="Sitecore.SessionProvider.Sql.SqlSessionStateProvider, Sitecore.SessionProvider.Sql" sessionType="Standard" connectionStringName="session" pollingInterval="2" compression="true" />
  </providers>
</sessionState>

As far as I can tell this is fine and should make the session last for 20 minutes.

Any thoughts on what could be wrong here?

8
  • Can you check the id of Tracker.Current.Contact.ContactId on the get and the post. I think you are sending two different sessions to these calls.
    – Chris Auer
    Commented Nov 21, 2017 at 2:45
  • Both have the same guid {f82c65fd-9111-45eb-b904-dd2a4f8b1286}
    – Diego
    Commented Nov 21, 2017 at 2:55
  • This are API calls or traditional MVC form controllers?
    – Chris Auer
    Commented Nov 21, 2017 at 3:00
  • Traditional Sitecore controller MVC renderings
    – Diego
    Commented Nov 21, 2017 at 3:01
  • 2
    Is this happening locally or only in a scaled environment?
    – Richard Seal
    Commented Nov 21, 2017 at 3:39

2 Answers 2

2

Try to pass the persistent paramenter:

Sitecore.Security.Authentication.AuthenticationManager.Login("extranet\\" + username, password, persistent: true, allowLoginToShell: false);
1
  • This didn't fix the issue
    – Diego
    Commented Nov 21, 2017 at 19:24
0

Sitecore asynchronously checks for robot detection and everything goes well then it reset the session according to your configuration.

Please observe on your first request, Sitecore will make an ajax request to check something, it this not happening properly then you have to figure out why this is not happening please check that visitor identification is working properly.

Probably sitecore will make request "/layouts/system/VisitorIdentificationCSS.aspx"

4
  • what you mean by "Please observe on your first request, Sitecore will make an ajax request to check something"? I haven't seen this happening as part of the request
    – Diego
    Commented Nov 21, 2017 at 16:31
  • Sitecore by default set ASP.Net session to 1 min for all users. Once Page is visited It makes a ajax cal to reset the session for 20 min /specified duration . Sitecore is doing this to detect the robots calls . You may also check the "Sitecore.Analytics.Tracking.config" there you will see a setting for default session .
    – Abhi
    Commented Nov 22, 2017 at 6:17
  • Observation - First checke Visital identification is enabled or not . make sure below code is added in your layout - @using Sitecore.Mvc.Analytics.Extensions @Html.Sitecore().VisitorIdentification() Clear cache -> open consol ,check requests , you will see a request "/layouts/system/VisitorIdentificationCSS.aspx" or any other similar request
    – Abhi
    Commented Nov 22, 2017 at 6:26
  • I have manually increased the session time on Sitecore.Analytics.Tracking.config to 15 minutes for example, and I do have the VisitorIdentification set on the layout. But still the session is getting cleared. When I post I see on the first line of the post method that the isAuthenticated is set to false
    – Diego
    Commented Nov 22, 2017 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.