共用方式為


Authentication in the Managed Client Object Models

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

By default, the managed client object models authenticate users by using their Windows credentials (DefaultCredentials). Optionally, you can change the authentication mode on the ClientContext object, and specify the use of Forms authentication when the server supports Forms authentication. A user must then supply a user name and password through properties on the client context object. Behind the scenes, Microsoft SharePoint Foundation 2010 calls the [Authentication Web service], obtains the correct cookie, and then makes the necessary object model calls. To run managed client code against a Forms authentication server, you must change to Forms authentication. This requirement does not apply to the JavaScript object model.

The managed client object models provide a ClientAuthenticationMode enumeration whose values are Anonymous, Default, and FormsAuthentication. To specify Forms authentication, use code similar to the following.

clientContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
clientContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication

In addition to setting the authentication mode, you must specify the user name and password information, as shown here.

FormsAuthenticationLoginInfo formsAuthInfo = new FormsAuthenticationLoginInfo("MyUser", "MyPassword");

clientContext.FormsAuthenticationLoginInfo = formsAuthInfo;
Dim formsAuthInfo As New FormsAuthenticationLoginInfo("MyUser", "MyPassword")

clientContext.FormsAuthenticationLoginInfo = formsAuthInfo

Note

In the example, if the account name and password contained in formsAuthInfo are sent in clear text, you must use HTTPS protocol instead of HTTP.

See Also

Concepts

Client Context as Central Object

Client Objects, Value Objects, and Scalar Properties

Data Retrieval Overview

SharePoint 2010 Client Object Model Guidelines

Differences Between Managed and JavaScript Object Models

Common Programming Tasks in the Managed Client Object Model

Other Resources

Client Class Library

JavaScript Class Library

Using the SharePoint Foundation 2010 Managed Client Object Model

Client Object Model Resource Center