User log out confirmation when logging in
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.
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\
""
""
";