Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SharePoint online:Windows客户端应用程序能否使用OAuth进行身份验证?_C#_Sharepoint_Oauth_Office365_Sharepoint Online - Fatal编程技术网

C# SharePoint online:Windows客户端应用程序能否使用OAuth进行身份验证?

C# SharePoint online:Windows客户端应用程序能否使用OAuth进行身份验证?,c#,sharepoint,oauth,office365,sharepoint-online,C#,Sharepoint,Oauth,Office365,Sharepoint Online,我们使用SharePoint客户端对象模型为SharePoint online构建Windows客户端应用程序。我们想使用OAuth来验证这个Windows客户机应用程序,但我们没有找到一种方法;MSDN上的文档也不明确 然而,当我使用链接https://.sharepoint.com/_layouts/appregnew.aspx,禁用了“在客户端计算机上运行的应用程序”选项,SharePoint online站点中是否有启用此选项的设置 您可以尝试使用应用程序作为“代理”,以oAuth作为解

我们使用SharePoint客户端对象模型为SharePoint online构建Windows客户端应用程序。我们想使用OAuth来验证这个Windows客户机应用程序,但我们没有找到一种方法;MSDN上的文档也不明确


然而,当我使用链接
https://.sharepoint.com/_layouts/appregnew.aspx
,禁用了“在客户端计算机上运行的应用程序”选项,SharePoint online站点中是否有启用此选项的设置

您可以尝试使用应用程序作为“代理”,以oAuth作为解决方案。本机应用程序用来直接连接到服务的“隐式”oAuth流目前还不可用。经过多次尝试,我终于找到了它

我想这不是最精彩的代码,但它是:

/// <summary>
/// Sets needed values
/// </summary>
/// <param name="clientId">The ClientId from the application</param>
/// <param name="redirectUri">The RedirectUri where the browser has to be send.</param>
/// <param name="resource">The source you want to access</param>
public OneDriveConnection(string clientId, string clientSecret, string redirectUri, string resource)
{
    this._clientId = clientId;
    this._redirectUri = Uri.EscapeDataString(redirectUri);
    this._resource = Uri.EscapeDataString(resource);
    this._clientSecret = clientSecret;
}

在这里,我找到了连接到SharePoint/Office365所需的请求:

这是一个解决方案,我们希望找到通过OAuth直接连接到SharePoint online的解决方案
/// <summary>
/// Authorizes the application
/// </summary>
public void Authorize()
{
    /* EXAMPLE: GET https://login.windows.net/common/oauth2/authorize
        * ?response_type=code
        * &client_id=acb81092-056e-41d6-a553-36c5bd1d4a72
        * &redirect_uri=https://mycoolwebapp.azurewebsites.net
        * &resource=https:%2f%2foutlook.office365.com%2f
        * &state=5fdfd60b-8457-4536-b20f-fcb658d19458 */

    string baseUri = "https://login.windows.net/common/oauth2/authorize";
    string authorizationUri = string.Format(baseUri
        + "?response_type=code"
        + "&client_id={0}"
        + "&redirect_uri={1}"
        + "&resource={2}"
        + "&state={3}", this._clientId, this._redirectUri, this._resource, "5fdfd60b-8457-4536-b20f-fcb658d19458");

    // Create the form
    Form webBrowserForm = new Form();
    webBrowserForm.MaximizeBox = false;
    webBrowserForm.MinimizeBox = false;
    webBrowserForm.Size = new System.Drawing.Size(580, 890);
    webBrowserForm.Text = "Webbrowser";
    webBrowserForm.FormBorderStyle = FormBorderStyle.FixedDialog;
    webBrowserForm.StartPosition = FormStartPosition.CenterScreen;

    // Create the WebBrowser
    WebBrowser webBrowser = new WebBrowser();
    webBrowser.Width = 580;
    webBrowser.Height = 890;
    webBrowser.Location = new System.Drawing.Point(0, 0);
    webBrowser.ShowPageSetupDialog();

    // Hook event to the webBrowser
    webBrowser.Navigated += webBrowser_Navigated;

    // Show the webBrowser and form to the user
    webBrowserForm.Controls.Add(webBrowser);
    webBrowserForm.Show();

    // Navigate to the authorizationUri
    webBrowser.Navigate(authorizationUri);
}
/// <summary>
/// When the url has code in it and contains a session_state get the code and do the GetTokenInformation
/// </summary>
private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
    if (e.Url.AbsoluteUri.Contains("code=") && e.Url.AbsoluteUri.Contains("session_state"))
    {
        string[] splited = e.Url.AbsoluteUri.Split(new char[] { '=', '&' });
        _code = splited[1];

        if (!string.IsNullOrWhiteSpace(_code)
            && !string.IsNullOrWhiteSpace(_redirectUri)
            && !string.IsNullOrWhiteSpace(_clientId))
        {
            GetTokenInformation(_code, _redirectUri, _clientId, _clientSecret);
        }
        else
        {
            _connected = false;
        }
    }
}
/// <summary>
/// This method gets tokeninformation: access_token, token_type, expires_in, resource, refresh_token, scope, id_token
/// </summary>
/// <param name="code">Code from the authorize request</param>
/// <param name="redirectUri">Reply url for your application</param>
/// <param name="clientId">Your applications client id in Windows Azure Directory</param>
/// <param name="clientSecret">Your applications client secret</param>
private void GetTokenInformation(string code, string redirectUri, string clientId, string clientSecret)
{
    // Get the token information that is set above in the constructor with the help of the clientId, clientSecret and code and as well as the redirectUri without it you can't connect to it otherwise it will crash if you don't do it like that
    string baseUri = "https://login.windows.net/common/oauth2/token";
    string parameters = string.Format("grant_type=authorization_code"
        + "&code={0}"
        + "&redirect_uri={1}"
        + "&client_id={2}"
        + "&client_secret={3}", code, redirectUri, clientId, clientSecret);
    string response = HttpPost(baseUri, parameters);

    if (!string.IsNullOrWhiteSpace(response))
    {
        _tokenInformation = JsonConvert.DeserializeObject<TokenInformation>(response);
        _connected = true;
    }
    else
    {
        _connected = false;
    }
}
[JsonObject(MemberSerialization.OptIn)]
class TokenInformation
{
    [JsonProperty(PropertyName = "access_token")]
    public string AccessToken { get; set; }

    [JsonProperty(PropertyName = "token_type")]
    public string TokenType { get; set; }

    [JsonProperty(PropertyName = "expires_in")]
    public int ExpiresIn { get; set; }

    [JsonProperty(PropertyName = "expires_on")]
    public int ExpiresOn { get; set; }

    [JsonProperty(PropertyName = "resource")]
    public string Resource { get; set; }

    [JsonProperty(PropertyName = "refresh_token")]
    public string RefreshToken { get; set; }

    [JsonProperty(PropertyName = "scope")]
    public string Scope { get; set; }

    [JsonProperty(PropertyName = "id_token")]
    public string IdToken { get; set; }
}