Example #1
0
        public static async Task OnOAuthRequest(Microsoft.AspNetCore.Http.HttpContext e, string next)
        {
            var method = Program.FindRequestMethod(e);

            if (next == "/query" && method == RequestHttpMethod.post)
            {
                await OAuthQuery.OnQueryRequest(e);
            }
            else if (next == "/authorize" && method == RequestHttpMethod.get)
            {
                await OAuthAuthorize.OnAuthorizeRequest(e);
            }
            else if (next == "/verify" && method == RequestHttpMethod.post)
            {
                await OAuthVerifyRequest.OnVerifyRequest(e);
            }
            else
            {
                throw new StandardError("Endpoint not found.", StandardErrorCode.NotFound);
            }
        }
Example #2
0
        private async Task <IdentityManager.Credential> generateCredential(string portalAppID)
        {
            // Get the ArcGIS Online or Portal URL to try to authenticate against
            string portalUrl = null;
            var    appPortal = MapApplication.Current != null ? MapApplication.Current.Portal : null;

            if (appPortal != null && !string.IsNullOrEmpty(appPortal.Url))
            {
                portalUrl = MapApplication.Current.Portal.Url;
            }
            else if (ArcGISOnline != null)
            {
                portalUrl = ArcGISOnline.Url;
            }
            else if (ArcGISOnlineEnvironment.ArcGISOnline != null)
            {
                portalUrl = ArcGISOnlineEnvironment.ArcGISOnline.Url;
            }

            IdentityManager.Credential cred = null;
            if (IdentityManager.Current != null && !string.IsNullOrEmpty(portalUrl))
            {
                portalUrl = portalUrl.TrimEnd('/');

                var options = new IdentityManager.GenerateTokenOptions()
                {
                    ProxyUrl = ProxyUrl,
                    TokenAuthenticationType = !string.IsNullOrEmpty(portalAppID) ? IdentityManager.TokenAuthenticationType.OAuthImplicit :
                                              IdentityManager.TokenAuthenticationType.ArcGISToken
                };

                if (!string.IsNullOrEmpty(portalAppID))
                {
                    var oauthAuthorize = new OAuthAuthorize()
                    {
                        UsePopup = true
                    };
                    options.OAuthAuthorize = oauthAuthorize;
                    var oauthClientInfo = new IdentityManager.OAuthClientInfo()
                    {
                        ClientId       = portalAppID,
                        OAuthAuthorize = oauthAuthorize,
                        RedirectUri    = HtmlPage.Document.DocumentUri.ToString()
                    };

                    var serverInfoRegistered = IdentityManager.Current.ServerInfos.Any(info => info.ServerUrl == portalUrl);
                    var serverInfo           = serverInfoRegistered ? IdentityManager.Current.ServerInfos.First(info => info.ServerUrl == portalUrl)
                        : new IdentityManager.ServerInfo();
                    serverInfo.ServerUrl               = portalUrl;
                    serverInfo.OAuthClientInfo         = oauthClientInfo;
                    serverInfo.TokenAuthenticationType = IdentityManager.TokenAuthenticationType.OAuthImplicit;
                    if (!serverInfoRegistered)
                    {
                        IdentityManager.Current.RegisterServers(new IdentityManager.ServerInfo[] { serverInfo });
                    }
                    cred = await IdentityManager.Current.GenerateCredentialTaskAsync(portalUrl, options);
                }
                else
                {
                    // Authenticate against ArcGIS Online/Portal to retrieve user token
                    cred = await IdentityManager.Current.GenerateCredentialTaskAsync(
                        portalUrl, viewModel.Username, viewModel.Password, options);
                }
            }
            return(cred);
        }