Check if user is logged in or not after OnlineIdConnectedStateChange has triggered
Asked Answered
R

1

7

I am working on a Windows 8.1 Metro app. The app gives a personalized experience to the user who is logged in to Windows with his Live account.

As provided in Windows, any user can connect/disconnect his live account to the PC via PC Settings -> Account. My requirement is to be notified at this event, i.e., whenever user connects/disconnects his live account to PC. Now, as per MSDN page (http://msdn.microsoft.com/en-us/library/windows/apps/hh977056.aspx) this event would trigger OnlineIdConnectedStateChange. I was able to implement a background task in the app that listens to the trigger and it is working perfectly fine. Everytime, a change occurs in connected Live account, the event listener is fired. But I am not able to determine the fact if the user logged in with some Live account or logged out from an already connected account.

I tried using Windows.Security.Authentication.OnlineId.OnlineIdAuthenticator as mentioned in (Metro App - How to detect if logged in with Live ID or Local Account) but this triggers the Authentication flow if no user is logged in. This is not what I want. I just want to know the fact if someone is logged in or not. I don't want to trigger an Auth flow.

Is there any way to determine the logged in/out status of the user when the OnlineIdConnectedStateChange event is fired in the background task?

Thanks for any help.

Revue answered 12/2, 2014 at 14:43 Comment(0)
D
3

You can always store the last userId of the user that successfully logged in. Then compare that Id with the current userId, if it's null then the user logged out, if not the same then it's a new user.

Edit:

  1. User opens the app 1.a: User logs in => store some flag or userId (some info basically) 1.b: User does not log in => store some null flag or null userId

  2. User closes app

  3. Backgroundworker picks up the event, based on the flag or info you can figure out if the user logged in or logged out.

Depending on what you store, you can use AppSettings or IsolatedStorage among other options to store the flag.

Dance answered 16/2, 2014 at 16:21 Comment(8)
My question is not to determine if the user is new or the same. It is to determine whether the action that triggered "OnlineIDConnectedStateChange" event was login or logout.Revue
You can still use this approach. If the Id is null and the event is fired, then it's a log in attempt and vice versa.Dance
Where would I get the Id from?Revue
When the user logs in for the first time successfully, you can get the Id along with the token.Dance
Take a look at this link: msdn.microsoft.com/en-us/library/live/hh826534.aspx Examine the loginResult, I played with it some time ago, but the userid resides somewhere there. If you want to keep it simpler, you could store just a flag as well, since you only want to know if it's a log out or login, and not concerned about user.Dance
Seems like you are not getting the picture here. What you are suggesting is how to check whether the user actually logged in or not when the app starts the auth flow. In that case, your link holds valid. Consider the case when my app is not running (not even in suspended state) and the user goes and disconnects his Microsoft account connected to Windows. In this case, this system event "OnlineIDConnectedStateChange" fires. Since, a background task from my app was listening to it, the event listener gets fired. Now how do I get this logged in info here?Revue
I think you're missing what I am saying. User launches your app and logs in, you can store a flag in the appsettings or isolatedstorage, user closes the app, then your backgroundtask detects a change, you can use that flag to figure out if the user logged in in your app before or not, assuming the backgroundtask has access to appsettings or isolatedstorage which I think it does.Dance
Oh! Dumb me! This might work. I will verify the appsettings access and will update soon. Meanwhile, can you edit your answer appropriately so that I can mark it as an answer if the approach worked? I am guessing it will.Revue

© 2022 - 2024 — McMap. All rights reserved.