0

I create session when user Login and use session to check access page, when i check session some time rerun null if i refresh page or navigate to other page the session is not null , it's not about specific page ,it's about method some time return null some time get session value , my method in web service and i call it use javascript

    [WebMethod(EnableSession = true)]            
    public object CheckIfLoggedin()
    {
        try
        {
            return new { Result = resultEnum.ok, Records = GetUserID() };

        }
        catch
        {
            return new { Result = resultEnum.error };
        }

    }
    private Guid? GetUserID()
    {
        if (Context.Session["User"] != null)
        {
            string userid = Context.Session["User"].ToString();
            if (userid != "")
                return new Guid(userid);
            else
                return null;
        }
        else
            return null;
    }

    function CheckIfLoggedin() {

    var status = 0;

    var ItemCount;
    $.ajax({

    type: "POST",
    url: "../../../../_layouts/15/TripPlannerFrontend/TripPlannerSrv.asmx/CheckIfLoggedin",

    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: true,
    success: function (res) {
        if (res.d.Result === 2) {
            var userid = res.d.Records;
            if (userid == null) {
                //
            }
            else {

                //window.location = "TripPlanner.aspx";
                var returnurl = GetQueryString("returnurl");
                if (returnurl != null && returnurl !== "")
                    window.location = returnurl;
                else
                    window.location = "TripPlanner.aspx";
            } 

        }
        else
            console.log("try again");
    },
    error: function (response) {
        console.log("try again");
    }
});
   };
3
  • Don't you have multiple Web front-end servers with load-balancing and no session-affinity?
    – Evariste
    Commented May 23, 2016 at 11:41
  • i have multiple Web front-end servers with load-balancing i do not think there is any session-affinity , do i need session-affinity ? Commented May 23, 2016 at 11:54
  • Context.Session , Session current.Session what is the different ? which one should i use use in Webservice? Commented May 28, 2016 at 14:38

1 Answer 1

0

As per the answer to my question in the comments, I think your problem is session affinity. If you have multiple Web front-ends in your SharePoint farm, and a load-balancer dispatching requests to one of these SP servers: a first request may be routed to server A, then the session is created, and when a second request is issued by the client, it may be redirected to server B, where the session does not exist.

In such situations, the best approach is to configure your load-balancer for "session affinity": this means that once a given client is assigned one of the Web front-ends, all subsequent requests will be redirected to the same server. Ask the Administrator of the load-balancer if this can be configured at his level (it depends on the technology used for load-balancing).

Another approach is to configure session at the servers level to be stored in a SQL database. See for instance http://sharepoint-community.net/profiles/blogs/asp-net-session-state-implementation-in-sharepoint for more info on this.

2
  • thank you i will try this, but the link is not correct Commented May 23, 2016 at 14:38
  • The link should be OK now. It was sharepoint-community.net/profiles/blogs/… with 'sharepoint' all lower-case, while the autocorrect had changed it to 'SharePoint'... and apparently the server at sharepoint-community.net process case-sensitive URLs! Crazy.
    – Evariste
    Commented May 23, 2016 at 15:01

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.