예제 #1
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);
        }
        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;
        }