public async Task <AuthenticationResult> AcquireTokenAsync(TokenAcquisitionRequest request)
        {
            AuthenticationResult token = null;

            if (null == request.Scopes)
            {
                request.Scopes = this.GetScopes();
            }

            if (!(request?.Prompt).HasValue || request?.Prompt == LoginPrompt.None)
            {
                this.AssertMsalService();
                try
                {
                    token = await this.MsalService.AcquireTokenSilentAsync(request);
                }
                catch { }
            }

            if (null == token)
            {
                token = await this.AuthenticateAsync(request);
            }

            return(token);
        }
        public async Task <AuthenticationResult> AuthenticateAsync(TokenAcquisitionRequest request)
        {
            request = request ?? new TokenAcquisitionRequest();

            request.Scopes = request.Scopes ?? this.ApplicationSettings.DefaultScopes;
            var module = await this.GetBlazoradeTeamsJSModuleAsync();

            await this.LocalStorage.SetItemAsync(request.CreateKey(this.ApplicationSettings.ClientId), request);

            var data = new Dictionary <string, object>
            {
                { "url", this.NavMan.ToAbsoluteUri(this.ApplicationSettings.LoginUrl) }
            };

            string result = null;
            AuthenticationResult token = null;

            using (var handler = new DotNetInstanceCallbackHandler <string>(module, "authentication_authenticate", data))
            {
                result = await handler.GetResultAsync(timeout : request.Timeout);
            }

            if (result?.Length > 0)
            {
                try
                {
                    token = JsonSerializer.Deserialize <AuthenticationResult>(result);
                }
                catch { }
            }

            if (null == token)
            {
                this.AssertMsalService();
                try
                {
                    token = await this.MsalService.AcquireTokenSilentAsync(fallbackToDefaultLoginHint : true);
                }
                catch { }
            }

            return(token);
        }
        protected async override Task OnAfterRenderAsync(bool firstRender)
        {
            await base.OnAfterRenderAsync(firstRender);

            if (firstRender)
            {
                await this.TeamsInterop.InitializeAsync();

                AuthenticationResult authResult = null;

                try
                {
                    authResult = await this.MsalService.HandleRedirectPromiseAsync();

                    if (null == authResult)
                    {
                        var request = new TokenAcquisitionRequest();
                        var key     = request.CreateKey(this.Options.ClientId);
                        request = await this.LocalStorage.GetItemAsync <TokenAcquisitionRequest>(key);

                        await this.LocalStorage.RemoveItemAsync(key);

                        await this.MsalService.AcquireTokenInteractiveAsync(request);
                    }
                }
                catch (Exception ex)
                {
                    await this.TeamsInterop.Authentication.NotifyFailureAsync(reason : ex.ToString());
                }

                if (null != authResult)
                {
                    var json = JsonSerializer.Serialize(authResult);
                    await this.TeamsInterop.Authentication.NotifySuccessAsync(result : json);
                }
            }
        }
Ejemplo n.º 4
0
 public static string CreateKey(this TokenAcquisitionRequest request, string clientId)
 {
     return($"{clientId}.blazorade-teams.token-request-info");
 }