2012-04-20 1 views
1

. (예에 대한 http://myportal/mysite/_layouts/application/default.aspx)에 널 :CurrentUser이 같은 코드를 가지고 셰어 포인트 2010 응용 프로그램 페이지 나 응용 프로그램 페이지, 셰어 포인트 서버에서 호스팅 한

protected void Page_PreLoad(object sender, EventArgs e) 
{   
    var userEmail = SPContext.Current.Web.CurrentUser.Email; 
} 

사용자가 URL에 의해 직접이 페이지를 얻기 위해 시도하는 경우 브라우저가 시작된 후 CurrentUser가 null이므로 예외가 나타납니다. 그러나 사용자가 처음으로 웹 사이트 (http : // myportal/mysite)로 이동 한 다음 응용 프로그램 페이지로 이동하면 CurrentUser는 null이 아닙니다. 그렇다면 CurrentUser 객체가 SPContext에서 초기화되지 않은 경우 어떻게 가져올 수 있습니까?

답변

0

RunWithElevatedPrivileges 코드 내 상승 된 SPWeb에서 현재 사용자 가져 오기. 이 코드를 사용해보십시오.

SPWeb site = SPContext.Current.Web; 
SPSecurity.RunWithElevatedPrivileges(delegate() 
{ 
    using (SPSite ElevatedsiteColl = new SPSite(site.Url)) 
    { 
     using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb()) 
     { 
      SPUser currUser = site.CurrentUser; //not the ElevatedSite.CurrentUser 
     } 
    } 
}); 
+0

Unfortunatelly site.CurrentUser is null입니다. – Tannheuser

0

음 ... 결코 늦지 않을수록 좋습니다. 나는 오늘이 같은 문제에 부딪쳤다. 이전 의견은 원래 포스터의 표현 된 문제를 다루지 않습니다. 문제는 _Layouts 폴더에 .ASPX 페이지를 게시 한 다음 Forms 또는 Claims 인증을 사용할 때 해당 사용자 정의 페이지를 세션에서 첫 번째 히트 (이전에 기억 된 로그인이 없음)로 만듭니다. SharePoint 인증은 기본적으로 (사용자가 LayoutsPageBase 클래스에서 상속 한 경우에도) 실행되지 않습니다. 다른 SharePoint 페이지 (예 : _Layouts/15/Settings.aspx)로 이동 한 다음 다시 돌아 오면 CurrentUser 으로 채워집니다. Reflector를 사용하여 진행 상황을보다 정확히 파악해야했습니다. 그것을 고치는 법. 당신이 CurrentUser가 == null의 경우, 당신은이 코드 줄을 추가 할 필요가 실현되면 짧은 대답입니다 : 내 경우에는

Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(new UnauthorizedAccessException()); 

를이 코드는 내가하는 데 사용되는 브라우저에 대한 도전/응답을 생성 로그인하고이 코드 줄 바로 뒤에 있으면 CurrentUser 개체가 올바르게 채워집니다. 여기 내 전체 기능이 다음과 같이 보입니다.

public static bool isAdminAuthorized() 
{ 
    Microsoft.SharePoint.SPContext oContext ; 
    Microsoft.SharePoint.SPWeb oWeb ; 
    Microsoft.SharePoint.SPUser oUser ; 
    try 
    { 
     oContext = Microsoft.SharePoint.SPContext.Current; 
    } 
    catch { throw new Exception("Can't obtain Sharepoint Context!"); } 
    try 
    { 
     oWeb = oContext.Web; 
    } 
    catch { throw new Exception("Can't obtain Sharepoint web!"); } 
    try 
    { 
     oUser = oWeb.CurrentUser; 
    } 
    catch { throw new Exception("Can't obtain Sharepoint current user!"); } 
    if (oUser == null) 
    { 
     Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(new UnauthorizedAccessException()); 
     oUser = oWeb.CurrentUser; 
    } 
    foreach (Microsoft.SharePoint.SPGroup oGroup in oUser.Groups) 
    { 
     if (oGroup.Name.ToUpper().Contains("OWNER")) 
     { 
      return true; 
     } 
    } 
    return false; 
} 
관련 문제