0

I use the below code

private static string userName = ConfigurationManager.AppSettings["UserID"].ToString();
private static string password = ConfigurationManager.AppSettings["Password"].ToString();
private static string siteUrl = ConfigurationManager.AppSettings["SiteUrl"].ToString();
private static SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(userName, convertToSecureString(password));
var ctx = new ClientContext(siteUrl);
ctx.Credentials = credentials;
static SecureString convertToSecureString(string password)
        {
            SecureString encryptedPassword = new SecureString();
            foreach (var c in password.ToCharArray()) encryptedPassword.AppendChar(c);
            return encryptedPassword;
        }

Now this returns a 403 forbidden error

What else is missing here?

This worked in a different site collection that I tried

Note: Difference in this site collection is that, the domain is different. i.e if the client site is sss@client.com, I am logging in using sss@mycompany.com, which is added as one of site collection admin by the client i.e I am an external user for their site collection

Also I checked the response and found the below error

enter image description here

1

Try it like this, looks like you are missing the authentication mode:

private static string userName = ConfigurationManager.AppSettings["UserID"].ToString();
private static string password = ConfigurationManager.AppSettings["Password"].ToString();
private static string siteUrl = ConfigurationManager.AppSettings["SiteUrl"].ToString();

using (ClientContext context = new ClientContext(siteUrl))
{
    SecureString securePassword = new SecureString();
    foreach (char c in password.ToCharArray())
    {
        securePassword.AppendChar(c);
    }

    context.AuthenticationMode = ClientAuthenticationMode.Default;
    context.Credentials = new SharePointOnlineCredentials(userName, securePassword);

    Web oWeb = context.Web;
    context.Load(oWeb, website => website.Title);
    context.ExecuteQuery();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.