Microsoft.ACE.OLEDB.12.0 doesn't work after applying impersonation in asp.net 4 - impersonation

In our application we allow user to upload excel file to server and import data from excel file to SQL.
We created one user and used that account for impersonation in application. We gave write and read excess to Upload folder to this user. Upload is working fine but at oconn.Open(); it throws error
But if i use admin account for impersonation it's working fine
using (OleDbConnection oconn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelproperties.filePath + "; Extended Properties=Excel 12.0;"))
{
oconn.Open();
}

The account needs access to the TEMP directory
https://support.microsoft.com/en-ph/help/825738/system-data-oledb-oledbexception-error-when-you-run-an-asp-net-web-app

Related

Using excel VBA to connect to azure sql with active directory authentication

I'm trying to connect in excel vba to a azure sql database using adodb and AD Integration but none of the connection strings I am using seem to work.
I am using ADODB.Connection in conjunction with the string below to connect to my sql server.
I've tried without using AD integrated security (that is, with a username and password) Public Const ConnectionString = "Provider=sqloledb;Data Source=company.database.windows.net; Initial Catalog=testdb; User Id=userinput; Password=pwinput;" and that works fine.
but trying with AD integration doesnt work (gives an error of invalid connection string attribute)
Public Const ConnectionString = "Provider=sqloledb;Data Source=company.database.windows.net; Initial Catalog=testdb;Authentication=ActiveDirectoryIntegrated;Encrypt=True"
I have tried with and without the end bit of Encrypt=True and still get the same error

Cannot bulk load because the file "File.csv" could not be opened. Operating system error code 5(Access is denied.)

I am having the following problem trying to access Azure Blob Store from Azure Sql Database and upload a single blob/image. Getting the above. I have configured SAS. Here are my findings:
If the container is public, the sql below works without credentials
When it is private it is possible to access the blob via blob url
from the browser (using the sas token there)
If I try to access it via Azure Sql Database (same token) I am
getting the same error as above:
"Failed to execute query. Error: Cannot bulk load because the file "POC.png" could not be opened. Operating system error code 5(Access is denied.)."
CREATE DATABASE SCOPED CREDENTIAL MyAzureBlobStorageCredential
WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = 'sv=2018-03-28....'
go
CREATE EXTERNAL DATA SOURCE MyAzureBlobStorage
WITH ( TYPE = BLOB_STORAGE,
LOCATION = 'https://poc.blob.core.windows.net/poc-container',
CREDENTIAL = MyAzureBlobStorageCredential);
go
INSERT INTO file111 (col2)
SELECT BulkColumn FROM OPENROWSET(
BULK 'POC.png',
DATA_SOURCE = 'MyAzureBlobStorage',
SINGLE_BLOB
) AS DataFile;
What I did was:
removed any not needed permissions and left only Read and List;
made sure that Start/End DateTime for the generated with a few days
in the past/far enough in the future;
made sure that the {container}{blobname} are the same lower/capital
letters as in the container;
there is no backslash at the end of the LOCATION
and it worked. Any of the bullet points which could cause the error message above.

"Keyword not supported: authentication" when using Active Directory Password against AAD in Azure Web App

I'm have an Azure Web App where I've set the connection string to point to an Azure SQL DB. I'd prefer to use an Azure Active Directory username/password for authentication so I used the following connection string:
Server=tcp:mydb.database.windows.net,1433;Initial Catalog=mytable;Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Authentication="Active Directory Password";
This causes my app to fail with the error:
Keyword not supported: 'authentication'
If I use the SQL Authentication (i.e. remove the Authentication="Active Directory Password" and change User ID and Password to an appropriate SQL username and password), everything works as expected.
Is it possible to use Active Directory Password with an Azure Web App connection string in order to use an AAD username/password to connect to an Azure SQL Db?
Please change the connection string as shown below:
string ConnectionString =
#"Data Source=n9lxnyuzhv.database.windows.net; Authentication=Active Directory Password; Initial Catalog=testdb; UID=bob#contoso.onmicrosoft.com; PWD=MyPassWord!";
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open();
Another option:
string ConnectionString =
#"Data Source=n9lxnyuzhv.database.windows.net; Authentication=Active Directory Integrated; Initial Catalog=testdb;";
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open();
Please note the text following the Authentication keyword.

Authenticate to Azure Sql Database without Active Directory? I Don't have SQL admin credentials either

I have learned a very painful lesson a that you cannot authenticate to Azure SQL database using Active Directory in the Cloud using Custom .Net activity.
At this point I'm looking for alternatives.
The code I'm using is:
string constring=
"Server = server,1433; " +
"Initial Catalog =dbname; " +
"Persist Security Info = False; " +
"MultipleActiveResultSets = False; " +
"Encrypt = True; TrustServerCertificate = False;" +
" Authentication = Active Directory Integrated";
SqlConnection con = new SqlConnection(constring);
con.Open()
Which fails in when deployed. I get an "unable to load asadsql.dll" error.
Any guidance would be greatly appreciated.
I'm using framework 4.7.2, is there anyway around this?
If you are able to run your SQL/AAD app successfully from your laptop and can connect to SQLDB, you should be able to run it from the Web app. I am slightly confused about ADF in this scenario

Access denied. You do not have permission to perform this action or access this resource

I have a DEV environment where I develop my code and I use a local Sharepoint.
I have a TEST environment where I deploy my application and use a Sharepoint on the TEST server.
I am using Sharepoint 2010.
I have a client object model that I am using to save a document in a sharepoint server.
I am using the Microsoft.Sharepoint.Client library for the following code;
public void Save(byte[] file, string url)
{
using (var context = new ClientContext(this.SharepointServer))
{
var list = context.Web.Lists.GetByTitle(this.DocumentLibrary);
var fileCreationInformation = new FileCreationInformation
{
Content = file,
Overwrite = true,
Url = url
};
var uploadFile = list.RootFolder.Files.Add(fileCreationInformation);
uploadFile.ListItemAllFields.Update();
context.ExecuteQuery();
}
}
}
When I run this code in my development environment, the document is saved on the Sharepoint as intended.
When I deploy to the test environment when the document is saved to a different Sharepoint server, I get the following exception:
Microsoft.SharePoint.Client.ServerUnauthorizedAccessException: Access denied. You do not have permission to perform this action or access this resource.
So there is a permissions issue here.
However when I change my code on the development server to save the document to the Sharepoint on the Test server, it works. The same Sharepoint that would not save the document from my deployed application on Test. I am also logging in with forms authentication with the same user name and password.
So why might this happen and where should I look to fix it?
I found what I was missing
I needed to set the credentials for the context;
context.Credentials = new NetworkCredential(this.UserName, this.Password, this.Domain);

Resources