简体   繁体   中英

Get sharepoint current User LoginName in C# MVC

I have 2 Apps :

  1. Sharepoint 2013 AS a portal web
  2. MVC App

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 ?

The information might needed :

  • Sharepoint login using windows authentication
  • Flow :
    1. User login in sharepoint web
    2. There is a button to open my MVC App in new tab (by URL)

For now i only know using REST API method. 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 ?

Any Method will be accepted as long as i can get the user 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.

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.

The answer by Sarel works only if your app is a sharepoint solution and is running in an sharepoint context. 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.

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.

In that case the user which opens the SharePoint site and click on the link to open your MVC app is the same user. Then you would be able to get the current use by HTTPContext in your MVC app.

System.Web.HttpContext.Current.User

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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