web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Supply chain | Supply Chain Management, Commerce
Answered

Getting 403 forbidden error - While trying to connect with Azure file share

(0) ShareShare
ReportReport
Posted on by 184

Hi All,

I am trying to connect with azure file share and getting 403 forbidden error "Remote server returned error" at line ( if (fileShare.Exists(null, null)))

Please let me know what would be the blocker here?

Also would like to know should I have to PASS the SAS token or the account key along with the storage account name to authenticate the request. Please confirm this

Below is my code . Please let me know If I am doing the right thing...

using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.File;

public class WTL_6DeeAzureBlobService
{
    #File
    #define.delimiterField(',')
    public static boolean establishCloudConnection()
    {
        #OCCRetryCount;

        System.IO.MemoryStream memoryStream;
        System.String          storageAccountName;
        System.String          keyValue;

        CloudStorageAccount    storageAccount;
        CloudFileClient        fileClient;
        CloudFileShare         fileShare;
        CloudFileDirectory     fileDirectoryRoot;
        CloudFileDirectory     fileDirectory;
        CloudFile              file;
 
        TextStreamIo           textStreamIo;
        Counter                counter;
        container              rec;

        storageAccountName     = "stcpd6dstgtstwe01";
        keyValue               =  WTL_APIStringBase64Converter.Base64Encoder::Base64Encoding("");
       
        
        var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, keyValue);
        storageAccount         = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);
        fileClient             = storageAccount.CreateCloudFileClient();
        fileShare              = fileClient.GetShareReference('azfile6dappsmb01');

        if (fileShare.Exists(null, null))
        {
            fileDirectoryRoot = fileShare.GetRootDirectoryReference();
            fileDirectory     = fileDirectoryRoot.GetDirectoryReference("");
  
            if (fileDirectory.Exists(null, null))
            {
                file = fileDirectory.GetFileReference('File.csv');
  
                if (file.Exists(null, null))
                {
                    memoryStream = new System.IO.MemoryStream();
                    file.DownloadToStream(memoryStream, null, null, null);
                    textStreamIo = TextStreamIo::constructForRead(memoryStream);
 
                    try
                    {
                        if (textStreamIo)
                        {
                            if (textStreamIo.status())
                            {
                                throw Global::error("@SYS52680");
                            }
 
                            textStreamIo.inFieldDelimiter(#delimiterField);
                            textStreamIo.inRecordDelimiter(#delimiterCRLF);
                            counter = 0;
 
                            while (!textStreamIo.status())
                            {
                                rec = textStreamIo.read();
 
                                if (conLen(rec))
                                {
                                    //vendABN = conPeek(rec, 1);
 
                                    //info(strFmt("%1", vendABN));
                                }
                            }
                        }
                    }
                    catch (Exception::Error)
                    {
                        error("An error occured. Please contact your system administrator.");
                    }
                }
            }
        }
  

            return true;
    }

    public static boolean readFile(CloudBlobContainer _cloudBlobCon)
    {
        return true;
    }

    public static boolean archiveFile()
    {
        return true;
    }

}

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    The problem looks clear to me. You get an authentication error because you aren't providing any access credentials. You have code for that, but you're using an empty string instead of an access key (at line 30). Copy an access key from Azure and use it at this place of your code

  • D365_Wibes Profile Picture
    184 on at

    Thanks a lot Martin for your valuable response.

    I have removed the key for security purposes before publishing the code here.

    I have tried passing the key but it did not work too.

    So I am confused whether I need to pass the SAS token or the account access key?

    Also I wonder if it is due to some firewall restrictions at the storage account?

    Can you please answer my above two questions? Please

  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    It's possible to use either an SAS token or an access key, but your current code needs an access key.

    If you want to use an SAS token instead, use the constructor of StorageCredentials() that accepts a single string. You can find this kind of information in documentation: StorageCredentials Constructors.

    It doesn't seem to be related to a firewall. The problem isn't that you can't reach the endpoint. You can, but you fail to authenticate.

  • D365_Wibes Profile Picture
    184 on at

    Thanks a lot Martin.

    I am sorry to ask this. Can you please give me sample x++ code how to pass the SAS token using the constructor of StorageCredentials please?

  • D365_Wibes Profile Picture
    184 on at

    using Microsoft.Azure;
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Blob;
    using Microsoft.WindowsAzure.Storage.File;
    
    public class WTL_6DeeAzureBlobService
    {
        #File
        #define.delimiterField(',')
        public static boolean establishCloudConnection()
        {
            #OCCRetryCount;
    
            System.IO.MemoryStream memoryStream;
            System.String          storageAccountName;
            System.String          keyValue;
    
            CloudStorageAccount    storageAccount;
            CloudFileClient        fileClient;
            CloudFileShare         fileShare;
            CloudFileDirectory     fileDirectoryRoot;
            CloudFileDirectory     fileDirectory;
            CloudFile              file;
     
            TextStreamIo           textStreamIo;
            Counter                counter;
            container              rec;
    
            storageAccountName     = "stcpd6dstgtstwe01";
            keyValue               =  WTL_APIStringBase64Converter.Base64Encoder::Base64Encoding("");
           
            
            var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, keyValue);
            storageAccount         = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);
            fileClient             = storageAccount.CreateCloudFileClient();
            fileShare              = fileClient.GetShareReference('azfile6dappsmb01');
    
            if (fileShare.Exists(null, null))
            {
                fileDirectoryRoot = fileShare.GetRootDirectoryReference();
                fileDirectory     = fileDirectoryRoot.GetDirectoryReference("");
      
                if (fileDirectory.Exists(null, null))
                {
                    file = fileDirectory.GetFileReference('File.csv');
      
                    if (file.Exists(null, null))
                    {
                        memoryStream = new System.IO.MemoryStream();
                        file.DownloadToStream(memoryStream, null, null, null);
                        textStreamIo = TextStreamIo::constructForRead(memoryStream);
     
                        try
                        {
                            if (textStreamIo)
                            {
                                if (textStreamIo.status())
                                {
                                    throw Global::error("@SYS52680");
                                }
     
                                textStreamIo.inFieldDelimiter(#delimiterField);
                                textStreamIo.inRecordDelimiter(#delimiterCRLF);
                                counter = 0;
     
                                while (!textStreamIo.status())
                                {
                                    rec = textStreamIo.read();
     
                                    if (conLen(rec))
                                    {
                                        //vendABN = conPeek(rec, 1);
     
                                        //info(strFmt("%1", vendABN));
                                    }
                                }
                            }
                        }
                        catch (Exception::Error)
                        {
                            error("An error occured. Please contact your system administrator.");
                        }
                    }
                }
            }
      
    
                return true;
        }
    
        public static boolean readFile(CloudBlobContainer _cloudBlobCon)
        {
            return true;
        }
    
        public static boolean archiveFile()
        {
            return true;
        }
    
    }

    HI Martin ,

    Please could help me with below , how to change it to pass the SAS token instead of account key also storage account name is there. Please I dont no how to do this.

  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    You need to change line 33. Instead of passing storageAccountName and keyValue to the constructor, use a single (string) value only: the SAS token:

    var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(sasToken);

  • D365_Wibes Profile Picture
    184 on at

    Thanks a lot Martin.

    So no need to pass the Storage account name any where else in the code right ?

  • D365_Wibes Profile Picture
    184 on at

    file-share-error.PNG

  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    I think you'll need to provide it in CloudStorageAccount's constructor, although I'm not 100% sure. Do you have any reason for complicating your life with it? If not, simply don't do it.

  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    Regarding your screenshot, please read the error message - it tells you what's wrong in your code.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Supply chain | Supply Chain Management, Commerce

#1
Siv Sagar Profile Picture

Siv Sagar 266 Super User 2025 Season 2

#2
Laurens vd Tang Profile Picture

Laurens vd Tang 213 Super User 2025 Season 2

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 140 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans