简体   繁体   中英

Authenticate to Sharepoint using cookies from ADFS

I need to access Sharepoint resources using CSOM.

I also need to authenticate such requests using cookies obtained from ADFS server.

Are there some third-party libraries that I can use to get the FedAuth and Rtfa cookies so I won't be forced to do the dirty job myself?

To access Sharepoint resources using CSOM, we don't need to get cookies, we can do authentication as below code:

    /// <summary>
    /// set authentication of SharePoint online
    /// </summary>
    /// <param name="clientContext"></param>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    public static void setOnlineCredential(ClientContext clientContext,string userName,string password)
    {
        //set the user name and password
        SecureString secureString = new SecureString();
        foreach (char c in password.ToCharArray())
        {
            secureString.AppendChar(c);
        }
        clientContext.Credentials = new SharePointOnlineCredentials(userName, secureString);           
    }

    /// <summary>
    /// set authentication of SharePoint on-premise
    /// </summary>
    /// <param name="clientContext"></param>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    /// <param name="domain"></param>
    public static void setClientCredential(ClientContext clientContext, string userName, string password, string domain)
    {
        clientContext.Credentials = new NetworkCredential(userName, password, domain);
    }

In addition, for SharePoint Online, We can use HttpClient to do authentication to get cookies and access token in C# code. we can refer to the below article:

SHAREPOINT ONLINE REMOTE AUTHENTICATION (AND DOC UPLOAD)

You can use WebBrowser to authenticate and then you can use the cookies to open your context like https://code.msdn.microsoft.com/remote-authentication-in-b7b6f43c

I was suggested by Microsoft to use this approach. Now I'm having issue implementing this on NET Core. as per current it is not supporting for Windows Form library. Do you have any better idea ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM