Creating an IUserSession that is used throughout my code I need a bit of help on if I have coded this class correctly.

I want to handle the setting of client cookie/client store.

public class UserSession : IUserSession
{

    private readonly IReadOnlySession _repo;
    public UserSession(IReadOnlySession repo)
    {
        _repo = repo;
    }

    private string _loginId;
    public string LoginID
    {
        get { return HttpContext.Current.User.Identity.Name; }
        set { _loginId = value; }
    }

    private string _companyIdentifier;
    public string CompanyIdentifier
    {
        get { return AuthenticateCookie.GetCompanyIdentifierFromTicket(LoginID); }
        set { _companyIdentifier = value; }
    }

    /// &lt;summary&gt;
    /// when the client store is set, this means the user has been changed
    /// &lt;/summary&gt;
    /// &lt;param name="loginId"&gt;&lt;/param&gt;
    /// &lt;param name="identifier"&gt;&lt;/param&gt;
    public void SetClientStore(string loginId, string identifier)
    {
        MyProfile = null;
        AuthenticateCookie.AddDetailsToCookie(loginId, identifier);

        LoginID = loginId;
        CompanyIdentifier = identifier;
    }

    public void Logout()
    {
        // we could throw an exception here?
        MyProfile = null;
        HttpContext.Current.User = null;
        FormsAuthentication.SignOut();
        //clear cookie values too?

    }

    private UserProfile _myprofile;
    public UserProfile MyProfile
    {
        get { return SetupProfile(); }
        private set { _myprofile = value; }
    }

    private UserProfile SetupProfile()
    {
        if (_myprofile == null)
        {
            if (string.IsNullOrEmpty(LoginID) || string.IsNullOrEmpty(CompanyIdentifier))
            {
                Logout();
                return null;
            }

            //call repo to get object...

            _myprofile = _repo.All&lt;User&gt;()
                .Where(x =&gt; x.Login == LoginID)
                .Join(_repo.All&lt;Profile&gt;().Where(x =&gt; x.IsActive), 
                      x =&gt; x.UserID, y =&gt; y.UserID,
                      (x, y) =&gt;
                          new
                            {
                                x.UserID, y.CompanyID, y.RoleID,
                            })
                .Join(_repo.All&lt;Company&gt;()
                        .Where(x =&gt; x.IsActive &amp;&amp; x.Identifier == CompanyIdentifier),
                    x =&gt; x.CompanyID, y =&gt; y.CompanyID,
                    (x, y) =&gt;
                        new
                          {
                              x.UserID, y.CompanyID, x.RoleID, x.IsSurveyor,
                              CompanyFriendlyName = y.Name,
                              CompanyType = y.Type
                          })
                .Join(_repo.All&lt;Role&gt;(), x =&gt; x.RoleID, y =&gt; y.RoleID,
                      (x, y) =&gt;
                          new
                            {
                                x.UserID, x.CompanyID, y.RoleName, x.CompanyFriendlyName, x.CompanyType
                            })
                .Join(_repo.All&lt;Subscription&gt;(),
                      x =&gt; x.CompanyID, y =&gt; y.CompanyID,
                      (x, y) =&gt;
                      new UserProfile
                          {
                              UserID = x.UserID,
                              CompanyID = x.CompanyID,
                              Role = x.RoleName.Convert&lt;RoleName&gt;(),
                              CompanyFriendlyName = x.CompanyFriendlyName,
                              LoginID = LoginID,
                              Identifier = CompanyIdentifier,
                              PricePlan = y.Name.Convert&lt;PricePlanType&gt;(),
                              CompanyType = x.CompanyType.Convert&lt;CompanyType&gt;()
                          })
                .SingleOrDefault();

            if (_myprofile == null)
                Logout();
        }

        return _myprofile;
    }
}
It's great that you separated the ASP.NET specific implementation from the interface. Two things to watch:


Don't forget to call Session.Abandon();Session.Clear(); in the Logout method.
SetupProfile contains a lot of code. This seems like business logic to me. Try to extract this code to its own class. An IUserService for instance. Or perhaps explicitly define Query Objects.