使用VB.NET为Sharepoint Online验证用户

时间:2017-03-28 01:54:10

标签: vb.net authentication sharepoint

我对将Sharepoint Online与vb.net程序联系起来非常陌生,任何建议肯定会有所帮助。

我添加了2 .dll 文件的参考, Microsoft.Sharepoint.Client Microsoft.Sharepoint.Client.Runtime 。它们是从SharePoint Server 2013 Client Components SDK 链接下载的。

下面是我的代码,

 Imports Microsoft.SharePoint.Client
 Imports Microsoft.SharePoint
 Imports Microsoft
 Imports System.Net
 Imports System.Security

Public Class FilesRetrievalFromSharePoint
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    Using ClientContext As New ClientContext("https://sample.sharepoint.com/Home.aspx")

        Dim password As New System.Security.SecureString()

        For Each c As Char In "pw@12345".ToCharArray()
            password.AppendChar(c)
        Next


        ClientContext.Credentials = New SharePointOnlineCredentials("user1@sample.com", password)
        Dim web As Web = ClientContext.Web

        ClientContext.Load(web)
        ClientContext.ExecuteQuery()

    End Using
End Sub

End Sub

下面的其他几个类
Private Class SharePointOnlineCredentials
    Implements ICredentials

    Private password As SecureString
    Private v As String

    Public Sub New(v As String, password As SecureString)
        Me.v = v
        Me.password = password
    End Sub

    Public Function GetCredential(uri As Uri, authType As String) As NetworkCredential Implements ICredentials.GetCredential
        Throw New NotImplementedException()
    End Function
End Class

我将在ExecuteQuery()点击的错误是

  

Microsoft.SharePoint.Client.dll中发生了'System.Net.WebException'类型的异常,但未在用户代码中处理

     

其他信息:远程服务器返回错误:(403)禁止。

我了解这是由于身份验证问题,而 Sharepoint 网址适用于公司中的授权用户。假设代码中的凭据属于授权用户。

是由于编码错误还是由于公司的IT安全政策?

1 个答案:

答案 0 :(得分:0)

更改线路 使用ClientContext作为新ClientContext(" https://sample.sharepoint.com/Home.aspx")

要 使用ClientContext作为新的ClientContext(" https://sample.sharepoint.com") 如果您打算使用其他网站 使用 使用ClientContext作为新ClientContext(" https://sample.sharepoint.com/sites/yoursite")

请勿使用该页面连接到该网站。