JamesMatson
JamesMatson

Reputation: 2922

UWP App with Azure AD Authentication - how to tell the signed in state of a user

I have built a small UWP app in C# that contains Azure AD authentication (to our corporate Azure AD account) based off the sample found here - https://github.com/Azure-Samples/active-directory-dotnet-native-uwp-wam/tree/master/NativeClient-UWP-WAM

I have the sample working successfully in my app, but I can't find a way to detect that a user is or isn't signed in (after the sign in is complete). I want to use this to ensure users can't navigate to other pages in the UWP app without being authenticated. If I was doing this in ASP.NET I would check something like the Context.Identity.User object. I just want to be able to do (in pseudo code)

Navigate to Page 1 if user signedIn = true, show page. If false, error/fail.

I've looked up user state, signed in state etc, but nothing really shows up.

Upvotes: 1

Views: 540

Answers (1)

Marilee Turscak - MSFT
Marilee Turscak - MSFT

Reputation: 7728

  1. Enable the User Account Information in the package.appmanifest enter image description here
  2. Then try:

    private async void Page_Loaded(object sender, RoutedEventArgs e)
    {
        IReadOnlyList<User> users = await User.FindAllAsync();
    
        var current = users.Where(p => p.AuthenticationStatus ==                                
        UserAuthenticationStatus.LocallyAuthenticated && 
                            p.Type == UserType.LocalUser).FirstOrDefault();
    
        // user may have username
        var data = await current.GetPropertyAsync(KnownUserProperties.AccountName);
        string displayName = (string)data;
    
        text1.Text = displayName; 
    }
    

https://learn.microsoft.com/en-us/uwp/api/Windows.System.User

Upvotes: 1

Related Questions