过去一周我一直在绞尽脑汁,无法解决对共享点提供商托管的应用程序进行正确身份验证的一些问题。
我目前正在为公司的 Sharepoint 在线开发一个 sharepoint 应用程序。我正在使用 Visual Studio 2013。我将该应用程序部署为公司的 Windows Azure 门户上的云服务。当我需要创建 HttpPost 时,一切都很顺利,然后应用程序无法进行身份验证。控制器的设计如下:
[SharePointContextFilter]
public ActionResult Index()
{
UserSingleton user_temp = UserSingleton.GetInstance();
User spUser = null;
SharePointContext spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
using (var clientContext = spContext.CreateUserClientContextForSPHost())
{
if (clientContext != null)
{
spUser = clientContext.Web.CurrentUser;
clientContext.Load(spUser, user => user.Title, user => user.Email);
clientContext.ExecuteQuery();
....code....
}
}
....code....
return View();
}
加载索引页面进展顺利,该操作创建了用户上下文,一切都很好。当我尝试提交 HttpPost 时,问题就出现了,如下所示:
[HttpPost]
[ValidateAntiForgeryToken]
[SharePointContextFilter]
public ActionResult GetService(System.Web.Mvc.FormCollection fc)
{
PlannedHours ph = this.PopulateModel(fc);
if (ph == null)
return View("NoInfoFound");
ViewData["PlannedHours"] = ph;
return View("Index");
}
当我通过发布按钮调用此按钮时,我收到“无法确定您的身份。请启动您网站上安装的应用程序再试一次。” Shared/Error.cshtml 视图。问题是,当我删除 [SharePointContextFilter] 时它会起作用,但这意味着请求不会通过 [SharePointContextFilter] 因此它没有经过正确的身份验证?或者是吗?因为它无法验证用户的合法性。
当我不删除 [SharePointContextFilter] 并调用帖子时,我注意到一件事,然后网址最终没有 {StandardTokens} 查询。是不是应该是这样的 - 我的意思是它类似于 hostname.com/Home/GetService,但是当我使用 actionlink 时,spcontext.js 总是将 {StandardTokens} 查询附加到基本 url - 类似于 hostname.com/Home /ActionNAme/?SPHostUrl=https%3A%2F%2FSHAREPOINTPAGEURL....
我注意到,我调用 hostname.com/Home/ActionNAme/ 而不附加查询,它无法通过 [SharePointContextFilter]。
我对 sharepoint 2013 和 MVC 5 ( Razor ) 相当陌生,所以如果您知道为什么我的 HttpPost 无法通过 [SharePointContextFilter],请尝试向我解释或提供任何建议。我尝试过使用 HttpGet 但是,当我调用具有 [SharePointContextFilter] 的 HttpGet 并附加 SPHostUrl= 令牌时,它可以工作。但是我无法使用[ValidateAntiForgeryToken]。由于 [SharePointContextFilter] 始终检查用户的合法性,因此在这样的应用程序中是否需要 [ValidateAntiForgeryToken]?我现在很困惑。网上有大量材料可供阅读,但没有什么可以解释何时附加这些标准令牌、何时使用 [SharePointContextFilter] 等。事实上,我是第一次开发 sharepoint 应用程序。在过去的三周里,我和生活一直在研究和编码。所以我的知识还很有限,回答时请记住这一点。预先感谢,我希望我能得到一些关于发生的事情的澄清!
- - - - - - - - - - - - - - -更新 - - - - - - - - - - --------------------
好的,快速更新。我发现了一些相当奇怪的事情。 SharePoint ContextFilter Attribute.cs
public class SharePointContextFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
Uri redirectUrl;
switch (SharePointContextProvider.CheckRedirectionStatus(filterContext.HttpContext, out redirectUrl))
{
case RedirectionStatus.Ok:
return;
case RedirectionStatus.ShouldRedirect:
filterContext.Result = new RedirectResult(redirectUrl.AbsoluteUri);
break;
case RedirectionStatus.CanNotRedirect:
filterContext.Result = new ViewResult { ViewName = "Error" };
break;
}
}
}
始终返回最后一个案例 ( RedirectionStatus.CanNotRedirect ),因为方法 SharePointContextProvider.CheckRedirectionStatus(filterContext.HttpContext, out redirectUrl) 包含一些我无法理解的内容。
首先:
Uri spHostUrl = SharePointContext.GetSPHostUrl(httpContext.Request);
if (spHostUrl == null)
{
return RedirectionStatus.CanNotRedirect;
}
好吧,我明白 - 如果 httpContext.Request 不包含 spHostUrl 它将无法重定向。由于某种原因,它必须存在。
但以下情况:
if (StringComparer.OrdinalIgnoreCase.Equals(httpContext.Request.HttpMethod, "POST"))
{
return RedirectionStatus.CanNotRedirect;
}
等等什么?!?不允许发帖?!!!这里发生了什么?我真的不知道我是否做错了什么或者什么?我什至可以使用 SharePointContext.cs 吗?我真的需要有人来澄清到底发生了什么......我将不胜感激!