繁体   English   中英

在C#MVC中获取共享点当前的用户LoginName

[英]Get sharepoint current User LoginName in C# MVC

我有2个应用程序:

  1. Sharepoint 2013作为门户网站
  2. MVC应用

如果我想从共享点登录一次,我如何才能从MVC应用程序中获取登录到共享点的当前用户?

该信息可能需要:

  • 使用Windows身份验证的Sharepoint登录
  • 流 :
    1. 用户在Sharepoint网站上的登录
    2. 有一个按钮可以在新标签页中打开我的MVC应用程序(按URL)

目前,我只知道使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM