Forum Discussion

Paolo Ajani's avatar
Paolo Ajani
Copper Contributor
May 08, 2018

Suddenly getting 401 error accessing SPS Online

Hi All. Since yesterday my c# code stopped working when trying to access SPS Online (and Project PWA in specific) through HttpWebRequest interface while CSOM continue to work as expected. in both c...
  • Sunil Guntupalli's avatar
    May 09, 2018

    Hi, 

     

    Just FYI, here is the piece of updated code i have used and now it is working fine.

    The one in BOLD is the new line of code that i have used to make it working.

     

    SecureString passWord = new SecureString();
    foreach (char c in m_Password.ToCharArray()) passWord.AppendChar(c);
    var credentials = new SharePointOnlineCredentials("UserName, passWord);
    string metaDataURL = "ProjectOnlineURL";
    var req = (HttpWebRequest)WebRequest.Create(metaDataURL);
    req.Credentials = credentials;

    req.Headers["X-FORMS_BASED_AUTH_ACCEPTED"] = "f";

    req.Headers.Add(HttpRequestHeader.Cookie, credentials.GetAuthenticationCookie(new Uri(metaDataURL)));
    var resp = (HttpWebResponse)req.GetResponse();

     

    Regards,

    Sunil G.

Resources