More in this section

Forums / Developing with Sitefinity / User log out confirmation when logging in

User log out confirmation when logging in

2 posts, 0 answered
  1. Thomas
    Thomas avatar
    223 posts
    Registered:
    19 Jan 2011
    16 Jan 2012
    Link to this post
    Hi,

    When you log into the back office, but are already logged in from somewhere else, you get an extra step before you log in: you have a warning that wants you to confirm you want to log yourself out first.

    Is there a way to disable this warning, and automatically log out the user from elsewhere? If that helps, I already have a custom login page (based on the default Sitefinity one), so I could add some code behind logic if there is no other way.

    Thanks.
  2. Lubomir Velkov
    Lubomir Velkov avatar
    688 posts
    Registered:
    03 Nov 2014
    17 Jan 2012
    Link to this post
    Hi Thomas,

    Well the problem is that SecurityManager isn't extensible and doesn't fire any events. You could use our services to try to login the user and logout him if it already is logged in - here is some sample code:

    public string InvokeWebMethod(string serviceUrl, string methodName, string httpMethod, byte[] data)
    {
        var request = (HttpWebRequest)WebRequest.Create(String.Concat(sitefinityHost, serviceUrl, methodName));
        request.Method = httpMethod;
        request.ContentType = "application/json";
        request.CookieContainer = new CookieContainer();
        if (cookies != null)
        {
            foreach (Cookie cookie in cookies)
                if (!cookie.Expired)
                    request.CookieContainer.Add(cookie);
        }
        if (data != null)
        {
            request.ContentLength = data.Length;
            using (var writer = request.GetRequestStream())
            {
                writer.Write(data, 0, data.Length);
            }
        }
        using (var response = (HttpWebResponse)request.GetResponse())
        {
            cookies = response.Cookies;
            using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }
    }
     
    public bool Logout()
    {
        return InvokeWebMethod(usersServiceUrl, logoutCurrentUserMethod, "GET", null) == "true";
    }
     
    public bool Logout(byte[] credentials)
    {
        return InvokeWebMethod(usersServiceUrl, logoutUserWithCredentialsMethod, "POST", credentials) == "true";
    }
     
    private bool AuthenticateRequest(string membershipProvider, string userName, string password, bool rememberMe)
    {
        var jsonData = String.Format(credentialsFormat, membershipProvider, userName, password, rememberMe.ToString().ToLower());
        var credentials = Encoding.UTF8.GetBytes(jsonData);
        string result = InvokeWebMethod(usersServiceUrl, authenticateMethod, "POST", credentials);
        switch (result)
        {
            case "0":
                return true;
            case "3":
            case "6":
            case "9":
                Response.Write("There is a user with the same credentials logged in the system form another application or browser.\r\n It will be logged out");
                if (Logout(credentials))
                    return InvokeWebMethod(usersServiceUrl, authenticateMethod, "POST", credentials) == "0";
                return false;
            default:
                return false;
        }
    }
    private const string credentialsFormat = @"{{""MembershipProvider"":""{0}"",""UserName"":""{1}"",""Password"":""{2}"",""Persistent"":{3}}}";
    private static CookieCollection cookies;
    private const string usersServiceUrl = "/Sitefinity/Services/Security/Users.svc/";
    private const string authenticateMethod = "Authenticate";
    private const string logoutCurrentUserMethod = "Logout";
    private const string logoutUserWithCredentialsMethod = "LogoutCredentials";
    private const string logoutOtherUserMethos = "Logout/{0}/{1}";
    private const string sitefinityHost = "http://localhost/sf_sitefinity";
    private const string createMethod = "create/00000000-0000-0000-0000-000000000000/?provider=Default";
    private const string usercreationFormat = @"{""UserName"":""testservice2"",""Password"":""testservice2"",""Email"":""testservice2@netfactory.de"",""PasswordQuestion"":"""",""PasswordAnswer"":"""",""Comment"":"""",""IsApproved"":true,""RolesOfUser"":[],""ProfileData"":"" {\""Telerik.Sitefinity.Security.Model.SitefinityProfile\"": {\""__type\"":\""Telerik.Sitefinity.Security.Model.SitefinityProfile\"",\""FirstName\"":\""testservice2\"",\""LastName\"":\""testservice2\"",\""About\"":null,\""PreferredLanguage\"":null,\""Avatar\"":null,\""Id\"":\""117F40E9-E74E-4A1C-AA34-C066D5A14678\"",\""ApplicationName\"":\""sitefinity/\"",\""LastModified\"":\""/Date(1319307482224)/\"",\""Owner\"":\""00000000-0000-0000-0000-000000000000\"",\""DateCreated\"":\""/Date(1319307482224)/\"",\""__providerName\"":\""Default\""}}""}";

    Kind regards,
    Lubomir Velkov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered