トップ回答者
SharePoint用アプリのサンプルでエラーが発生します

質問
-
http://msdn.microsoft.com/JA-JP/library/office/fp142381.aspx
にあります、サンプルを実行してみましたが、
F5キーを押して実行した後、Webページが表示されます。
その後、Populate Data を押すとエラーが発生します。WebExceptionはユーザーコードによってハンドルされませんでした。
リモートサーバーがエラーを返しました:(401)許可されていませんWebページには
Could not find a context token. というメッセージが表示されています。clientContext.ExecuteQuery(); でデバックが停止します。
どのように解決すべきでしょうか?よろしくお願いします。
public partial class Default : System.Web.UI.Page { SharePointContextToken contextToken; string accessToken; Uri sharepointUrl; string siteName; string currentUser; List<string> listOfUsers = new List<string>(); List<string> listOfLists = new List<string>(); // The Page_load method fetches the context token and the access token. // The access token is used by all of the data retrieval methods. protected void Page_Load(object sender, EventArgs e) { TokenHelper.TrustAllCertificates(); string contextTokenString = TokenHelper.GetContextTokenFromRequest(Request); if (contextTokenString != null) { contextToken = TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority); sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]); accessToken = TokenHelper.GetAccessToken(contextToken, sharepointUrl.Authority).AccessToken; CSOM.CommandArgument = accessToken; } else if (!IsPostBack) { Response.Write("Could not find a context token."); return; } } // This method retrieves information about the host web by using the CSOM. private void RetrieveWithCSOM(string accessToken) { if (IsPostBack) { sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]); } ClientContext clientContext = TokenHelper.GetClientContextWithAccessToken( sharepointUrl.ToString(), accessToken); //Load the properties for the web object. Web web = clientContext.Web; clientContext.Load(web); clientContext.ExecuteQuery(); //Get the site name. siteName = web.Title; //Get the current user. clientContext.Load(web.CurrentUser); clientContext.ExecuteQuery(); currentUser = clientContext.Web.CurrentUser.LoginName; //Load the lists from the Web object. ListCollection lists = web.Lists; clientContext.Load<ListCollection>(lists); clientContext.ExecuteQuery(); //Load the current users from the Web object. UserCollection users = web.SiteUsers; clientContext.Load<UserCollection>(users); clientContext.ExecuteQuery(); foreach (User siteUser in users) { listOfUsers.Add(siteUser.LoginName); } foreach (List list in lists) { listOfLists.Add(list.Title); } } protected void CSOM_Click(object sender, EventArgs e) { string commandAccessToken = ((LinkButton)sender).CommandArgument; RetrieveWithCSOM(commandAccessToken); WebTitleLabel.Text = siteName; CurrentUserLabel.Text = currentUser; UserList.DataSource = listOfUsers; UserList.DataBind(); ListList.DataSource = listOfLists; ListList.DataBind(); } }
- 移動 星 睦美 2013年9月27日 5:32 SP - 開発とプログラミング から
2013年9月27日 3:25
回答
-
ご自分の環境(OS, IIS, ASP.NET, Visual Studio、ブラウザのバージョンなど)ぐらいは書いていただけませんか?
中身は詳しく読んでませんが、IIS で動かして試験しているとすると、IIS のワーカープロセスに適切な権限を与えてないことによるトラブルが多いようです。
だから 401(アクセス拒否)エラーが出るのではないかと思いますが、そのあたりは大丈夫ですか?
- 回答としてマーク i-POWER Okuda 2013年9月27日 5:54
2013年9月27日 3:52
すべての返信
-
ご自分の環境(OS, IIS, ASP.NET, Visual Studio、ブラウザのバージョンなど)ぐらいは書いていただけませんか?
中身は詳しく読んでませんが、IIS で動かして試験しているとすると、IIS のワーカープロセスに適切な権限を与えてないことによるトラブルが多いようです。
だから 401(アクセス拒否)エラーが出るのではないかと思いますが、そのあたりは大丈夫ですか?
- 回答としてマーク i-POWER Okuda 2013年9月27日 5:54
2013年9月27日 3:52 -
i-POWER Okuda さん、こんにちは
フォーラム オペレーターの星 睦美です。サンプルのプログラムがSharePoint 2013 向けの内容ですので、私のほうで こちらの質問をSharePoint 2013 フォーラムに移動させていただきますね。
※フォーラムの回答がお役にたちましたら、今後もユーザー同士の情報交換がより活発になるように、投稿者からの[回答としてマーク]をお願いします。フォーラム オペレーター 星 睦美 - MSDN Community Support
2013年9月27日 5:32 -
まだ解決してないようですが、その場合は「回答としてマーク」はつけないほうがいいです。マークがついていると注目されなくなって回答が集まりにくくなりますから。
> IIS (Express)のワーカープロセスの権限を確認してみます。
VS2012 で IIS 7.5 Express を使用して開発しているのですね。その場合、ワーカープロセスは VS2012 を動かしているアカウントになるはずです。
たぶん、そのアカウントは開発マシンにログインしているユーザーアカウントになっているはずで、それが必要なクレデンシャルを持っていないから 401(アクセス拒否)エラーが出ているのでしょう。
以下のスレッドで同様な問題と解決策が報告されていますので参考にしてください。
ASP.net WEBアプリでctrl+f5で実行すれば問題ないですが発行すればエラーになります
http://social.msdn.microsoft.com/Forums/ja-JP/3394619e-b71a-4b5e-9a0a-a06d6f46ec51/aspnet-webctrlf5
上記のケースは、開発マシンにログインしているユーザーアカウントにはクレデンシャルがあったので ASP.NET 開発サーバーでは問題なく動いたが、IIS のワーカープロセスはクレデンシャルを持っていなかったので 401 エラーになったというものです。今回のケースとはちょっと違うので注意してください。開発マシンで動くようにしても、運用環境で IIS 上で動かした場合、IIS のワーカープロセスにクレデンシャルを与えないとまた 401(アクセス拒否)エラーが出ると思います。
- 編集済み SurferOnWww 2013年9月27日 7:20 誤字訂正
2013年9月27日 7:17