Ejemplo n.º 1
0
        private async Task AuthPocketAsync()
        {
            if (!AuthPocketViaSavedAccessToken())
            {
                pocketClient = new PocketClient(Secrets.PocketAPIConsumerKey);
                Uri          callbackUri  = WebAuthenticationBroker.GetCurrentApplicationCallbackUri();
                RequestToken requestToken = await pocketClient.ObtainRequestTokenAsync(
                    callbackUri);

                Uri requestUri = pocketClient.ObtainAuthorizeRequestTokenRedirectUri(requestToken, callbackUri);
                WebAuthenticationResult result = await WebAuthenticationBroker.AuthenticateSilentlyAsync(requestUri);

                if (result.ResponseStatus != WebAuthenticationStatus.Success)
                {
                    result = await WebAuthenticationBroker.AuthenticateAsync(
                        WebAuthenticationOptions.None,
                        requestUri);
                }

                AccessToken token = await pocketClient.ObtainAccessTokenAsync(requestToken);

                ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
                string tokenString = JsonSerializer.Serialize(token);
                localSettings.Values.Add("accessToken", tokenString);
            }
        }
Ejemplo n.º 2
0
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
    public async override Task <IToken> LoginAsync()
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
    {
        string accessToken = string.Empty;

        Logger.Log("Logging in with WebAuthenticationBroker...");

#if ENABLE_WINMD_SUPPORT
        var redirectUri = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().AbsoluteUri;

        var state = Guid.NewGuid().ToString();
        var nonce = Guid.NewGuid().ToString();

        string url = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize";

        var uri = new Uri($"{url}?" +
                          $"client_id={ClientId}&" +
                          $"scope={Uri.EscapeDataString("https://sts.mixedreality.azure.com/mixedreality.signin")} openid&" +
                          $"response_type=token&" +
                          $"state={Uri.EscapeDataString(state)}&" +
                          $"nonce={Uri.EscapeDataString(nonce)}&" +
                          $"redirect_uri={Uri.EscapeDataString(redirectUri)}");
        //+ $"prompt=select_account");

        bool useEnterpriseAuth = false;
        var  options           = useEnterpriseAuth == true ? WebAuthenticationOptions.UseCorporateNetwork : WebAuthenticationOptions.None;

        Logger.Log("Using Start URI: ");
        Logger.Log(uri.AbsoluteUri);

        Logger.Log("Waiting for authentication...");
        try
        {
            WebAuthenticationResult result;

            Logger.Log("Trying silent auth");
            result = await WebAuthenticationBroker.AuthenticateSilentlyAsync(uri);

            Logger.Log($"Silent Auth result: {result.ResponseStatus.ToString()}");

            if (result.ResponseStatus != WebAuthenticationStatus.Success)
            {
                Logger.Log($"{result.ResponseData} : [{result.ResponseErrorDetail.ToString()}]");
                Logger.Log("Trying UI auth");
                result = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, uri);//, new Uri(redirectUri));
            }

            Logger.Log("Waiting for authentication complete.");

            switch (result.ResponseStatus)
            {
            case WebAuthenticationStatus.Success:
                Logger.Log("Authentication Successful!");
                Logger.Log("Received data:");
                Logger.Log(result.ResponseData);
                accessToken = result.ResponseData.Split('=')[1];
                break;

            case WebAuthenticationStatus.UserCancel:
                Logger.Log("User cancelled authentication. Try again.");
                break;

            case WebAuthenticationStatus.ErrorHttp:
                Logger.Log("HTTP Error. Try again.");
                Logger.Log(result.ResponseErrorDetail.ToString());
                break;

            default:
                Logger.Log("Unknown Response");
                break;
            }
        }
        catch (Exception e)
        {
            Logger.Log($"Unhandled {e} - {e.Message}");
        }
#endif

        AADToken = accessToken;
        return(new AADToken(accessToken));
    }