简体   繁体   English

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

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

I have 2 Apps : 我有2个应用程序:

  1. Sharepoint 2013 AS a portal web Sharepoint 2013作为门户网站
  2. MVC App MVC应用

If i want a single sign on from sharepoint, how can i get the current user that logged in to sharepoint from my MVC App ? 如果我想从共享点登录一次,我如何才能从MVC应用程序中获取登录到共享点的当前用户?

The information might needed : 该信息可能需要:

  • Sharepoint login using windows authentication 使用Windows身份验证的Sharepoint登录
  • Flow : 流 :
    1. User login in sharepoint web 用户在Sharepoint网站上的登录
    2. There is a button to open my MVC App in new tab (by URL) 有一个按钮可以在新标签页中打开我的MVC应用程序(按URL)

For now i only know using REST API method. 目前,我只知道使用REST API方法。 Code Existing : 现有代码:

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);

But in my code is only get the user that i assigned in network credential. 但是在我的代码中仅获得我在网络凭据中分配的用户。 How can i get the current user that logged in to sharepoint from my MVC App ? 我如何从MVC应用程序中获取登录到共享点的当前用户?

Any Method will be accepted as long as i can get the user LoginName. 只要我可以获取用户LoginName,任何方法都将被接受。 Just tell me what should i do. 告诉我该怎么办。

Thank you 谢谢

I have not test this but this might put you into the correct direction. 我尚未对此进行测试,但这可能会使您进入正确的方向。

using (SPSite site = new SPSite(SPContext.Current.Site.ID)){
using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
   string userName = web.CurrentUser.LoginName;
}}

hope that helps. 希望能有所帮助。

Edit 编辑

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();    
        }    

    }    
}

Hope this explain more about the hosted provider Storm was talking about. 希望这能解释有关Storm所讨论的托管提供商的更多信息。

That is technically not possible. 从技术上讲这是不可能的。 When your MVC app is not a ProviderHostedApp than you cannot get the user which is logged in to the SharePoint portal. 当您的MVC应用不是ProviderHostedApp时,您将无法获得登录到SharePoint门户的用户。

The answer by Sarel works only if your app is a sharepoint solution and is running in an sharepoint context. 仅当您的应用程序是共享点解决方案并且在共享点上下文中运行时,Sarel的答案才有效。 Otherwise you can only built a new SPSite or SPWeb object with the current use, yes, but it is the user which is running/using your MVC app. 否则,您只能使用当前用途构建一个新的SPSite或SPWeb对象,是的,但是正在运行/使用MVC应用程序的是该用户。

Update 更新

When the user open your app on button click out of the sharepoint site, than you have to ensure that the IIS configuration for the MVC app is set to Windows Authentication and User impersonation. 当用户在按钮上打开您的应用程序时,从共享点网站上单击鼠标,则必须确保将MVC应用程序的IIS配置设置为Windows身份验证和用户模拟。

In that case the user which opens the SharePoint site and click on the link to open your MVC app is the same user. 在这种情况下,打开SharePoint网站并单击链接以打开您的MVC应用程序的用户是同一用户。 Then you would be able to get the current use by HTTPContext in your MVC app. 然后,您将能够在MVC应用程序中通过HTTPContext获得当前的使用。

System.Web.HttpContext.Current.User

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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