How to Get Current UserID?

What is the equivalent of Window's "System.Security.Principal.WindowsIdentity.GetCurrent().Name" for IOS and Android in Xamarin? Is there a platform agnostic operation?


  • alaskanroguealaskanrogue USMember ✭✭✭

    Actually, I am looking to perform this on non-mobile devices.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @alaskanrogue said:
    Actually, I am looking to perform this on non-mobile devices.

    Keep in mind that the eco-system however is geared for cross-platform and mobile development. If you choose to only work on UWP and MacOS desktops that's your choice to only support a subset of all the OSes... but you still have to code as if were were supporting them all because the Xamarin infrastructure doesn't know any different.

    I've never had a need, but I would think there are two routes:
    1) Make a DependecyService and create your service in each platform project you want to support
    2) Just make the Xamarin.Forms app, do what you need to in the app class, and don't give it any UI /don't push any page.

  • alaskanroguealaskanrogue USMember ✭✭✭

    Your link really didn't address my question? If I choose to make a "Forms" app, is there a common function for the current UserID across all of the OS'es?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited October 2017

    Well... To start with.. Is there even a current user ID across all OSes? When you boot your Samsung android tablet do you have to log in with a user that has a name - or just a passcode to unlock the device? What is the user ID on your iPhone? There might be an iTunes ID... There might be a contact in the address book that is designated as the "me" card... there might be an iCloud ID used for data syncing, but that's not a user ID for who is logged into the device.

    This is the point i tried to make - that you have to think as if you are making mobile apps even if you choose not to support the iPhone or a Samsung watch, because the eco-system is a lowest-common denominator mindset.

    If you want to use the Android Playstore ID as the user ID on Android, and the iTunes ID (or the iCloud id) on apple then you can make a DependencyService for each, and feed your chosen ID up to the PCL as a way to normalize your intended purpose.

  • alaskanroguealaskanrogue USMember ✭✭✭

    At this time, the intended deployment targets for the app are enterprise computing devices across the three OS's, i.e. PC's and Tablets. The need for the app doesn't exist on personal or mobile devices and hence no need to obtain the current UserID.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I don't think that business rule affects the suggested advice. You have decide what the user ID is in your particular use-case - then pass it up from your DependencyService to the PCL layer.

    If your choice for that is some corporate assigned ID, then cool. If it is user login as defined in KNOX, okay. If it is the user log into to your enterprise servers through REST, okay.

    The point is there is no concept as a "user id" in several of these platforms, and thus no cross-platform support for it. You'll have to implement your own based on your business' concept of what the userID actually is in your use-case.

  • alaskanroguealaskanrogue USMember ✭✭✭

    Which "no concept as a "user id" in several of these platforms" platforms are you referring to?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Now we are just going around in circles. I know I said just a couple posts up that you don't log into your iPhone. There is no "user ID". There is an account used for iTunes. And a different one for iCloud sync. And a contact in your phone book that you can designate as "me". But you don't actually log into your device with a user ID/name/password etc.

    Now you say> But I'm not targeting iOS
    THen I say> But the Xamarin eco-system is built around mobile and is lowest-common-denominator of feature for all the platforms it supports
    Then you act like you don't know what I'm talking about, and we circle back around.

    I'm done. I wish you the best of luck with your project, but I'm bowing out of this conversation.

  • alaskanroguealaskanrogue USMember ✭✭✭

    Clint, what I said was I was not targeting phones or personal devices. What I was seeking was the equivalent operations in the different OS's for WindowsIdentity.GetCurrent().Name.

    Thanks for your interaction however. Good luck to you also.

  • Damien_DoumerDamien_Doumer Member ✭✭

    Hi, @alaskanrogue Instead of looking for a user ID, you could instead have the user's email address, which is also unique for every user. Here is a blog post which describes how to get such information from Android, and UWP. The code also works if yuoj will want to get emails. On iOS, getting user information is not that easy.

Sign In or Register to comment.