[英]Get sharepoint current User LoginName in C# MVC
我有2個應用程序:
如果我想從共享點登錄一次,我如何才能從MVC應用程序中獲取登錄到共享點的當前用戶?
該信息可能需要:
目前,我只知道使用REST API方法。 現有代碼:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(HostURL + "/_api/Web/CurrentUser?Select=id");
request.Method = "GET";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.UseDefaultCredentials = true;
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential(UserName, Password, DomainName);
但是在我的代碼中僅獲得我在網絡憑據中分配的用戶。 我如何從MVC應用程序中獲取登錄到共享點的當前用戶?
只要我可以獲取用戶LoginName,任何方法都將被接受。 告訴我該怎么辦。
謝謝
我尚未對此進行測試,但這可能會使您進入正確的方向。
using (SPSite site = new SPSite(SPContext.Current.Site.ID)){
using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
string userName = web.CurrentUser.LoginName;
}}
希望能有所幫助。
編輯
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCApp.Controllers
{
public class EmployeeController : Controller
{
[SharePointContextFilter]
public ActionResult Index()
{
User spUser = null;
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
using (var clientContext = spContext.CreateUserClientContextForSPHost())
{
if (clientContext != null)
{
spUser = clientContext.Web.CurrentUser;
clientContext.Load(spUser, user => user.Title);
clientContext.ExecuteQuery();
ViewBag.UserName = spUser.Title;
}
}
return View();
}
}
}
希望這能解釋有關Storm所討論的托管提供商的更多信息。
從技術上講這是不可能的。 當您的MVC應用不是ProviderHostedApp時,您將無法獲得登錄到SharePoint門戶的用戶。
僅當您的應用程序是共享點解決方案並且在共享點上下文中運行時,Sarel的答案才有效。 否則,您只能使用當前用途構建一個新的SPSite或SPWeb對象,是的,但是正在運行/使用MVC應用程序的是該用戶。
更新
當用戶在按鈕上打開您的應用程序時,從共享點網站上單擊鼠標,則必須確保將MVC應用程序的IIS配置設置為Windows身份驗證和用戶模擬。
在這種情況下,打開SharePoint網站並單擊鏈接以打開您的MVC應用程序的用戶是同一用戶。 然后,您將能夠在MVC應用程序中通過HTTPContext獲得當前的使用。
System.Web.HttpContext.Current.User
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.