2017-05-26 82 views
0

我是Sharepoint的完整noob。 2周前我剛開始學習sharepoint,因爲我的老闆讓我參加了一個共享點項目。我必須在現有的基於聲明的Intranet Web應用程序中實現2FA和FBA。我雖然只是通過研究來完成這項任務,但我還沒有爲我的問題找到明確的指導或答案。Sharepoint 2013 - FBA和2FA與自定義登錄頁

下面是我的一些任務:

1)添加基於表單的身份驗證的網站,並使用自定義登錄頁面。

2)認證

  • 檢查用戶名和密碼登錄後AD。
  • 如果有效,必須向第三方提供商請求OTP代碼爲 2FA。
  • 用戶通過兩者後進行身份驗證。

配置和自定義登錄頁面沒有太大麻煩,並且沒有太多時間完成它們。但我被困在2FA部分。

1)如何定製認證過程?我不記得我在哪裏得到了下面的代碼,但我真的希望我能夠做到這一點。那麼,我可以對它做些什麼,或者我走錯了路?我非常感謝任何幫助,並提前致謝。

protected void btnLogin_Click(object sender, EventArgs e) 
    { 
     bool status = SPClaimsUtility.AuthenticateFormsUser(
      Context.Request.UrlReferrer, 
      txtUsername.Value.ToString(), 
      txtPassword.Value.ToString()); 

     if (!status) // if auth failed 
     { 
      lblInvalid.InnerText = "Wrong Username or Password"; 
      lblInvalid.Visible = true; 
     } 
     else //if success 
     {  
    //What do I do here to change the user back to not authenticated? 

     } 
    } 

回答

0

正確登錄後設置聯合認證cookie域。

HttpCookie httpCookie = current.Response.Cookies["FedAuth"]; 
httpCookie.Domain = "." + ConfigurationManager.AppSettings["yourdomain"]; 

登出方法是比較複雜的,很久以前我基於SharePoint SignOut根據我的解決方案上this post

並註銷方法(對不起,變量名,但我反編譯我的舊的DLL)頁面,然後從後修復:

public static void SignOut(SPSite site, SPWeb web, IClaimsPrincipal principal) 
{ 
    HttpContext current = HttpContext.Current; 
    if (current.Session != null) 
    { 
     current.Session.Clear(); 
    } 
    string value = string.Empty; 
    if (current.Request.Browser["supportsEmptyStringInCookieValue"] == "false") 
    { 
     value = "NoCookie"; 
    } 
    HttpCookie httpCookie = current.Request.Cookies["WSS_KeepSessionAuthenticated"]; 
    bool flag = false; 
    for (int i = 0; i < current.Request.Cookies.Count; i++) 
    { 
     HttpCookie httpCookie2 = current.Request.Cookies.Get(i); 
     if (httpCookie2.Name == "FedAuth" && !flag) 
     { 
      flag = true; 
      httpCookie2.Domain = WebConfigurationManager.AppSettings["yourdomain"]; 
     } 
    } 
    if (httpCookie != null) 
    { 
     httpCookie.Value = value; 
     current.Response.Cookies.Remove("WSS_KeepSessionAuthenticated"); 
     current.Response.Cookies.Add(httpCookie); 
    } 
    HttpCookie httpCookie3 = current.Request.Cookies["MSOWebPartPage_AnonymousAccessCookie"]; 
    if (httpCookie3 != null) 
    { 
     httpCookie3.Value = value; 
     httpCookie3.Expires = new DateTime(1970, 1, 1); 
     current.Response.Cookies.Remove("MSOWebPartPage_AnonymousAccessCookie"); 
     current.Response.Cookies.Add(httpCookie3); 
    } 
    SPIisSettings iisSettingsWithFallback = site.WebApplication.GetIisSettingsWithFallback(site.Zone); 
    if (iisSettingsWithFallback.UseClaimsAuthentication) 
    { 
     string iPUrl = Authentication.GetIPUrl(principal); 
     if (iPUrl != string.Empty) 
     { 
      string str = HttpUtility.UrlEncode(SPContext.Current.Site.RootWeb.Url); 
      string url = iPUrl + "?wa=wsignout1.0&wreply=" + str; 
      FederatedAuthentication.SessionAuthenticationModule.SignOut(); 
      if (current.Session != null) 
      { 
       current.Session.Abandon(); 
      } 
      current.Response.Redirect(url); 
     } 
     else 
     { 
      FederatedAuthentication.SessionAuthenticationModule.SignOut(); 
      int num = 0; 
      foreach (SPAuthenticationProvider current2 in iisSettingsWithFallback.ClaimsAuthenticationProviders) 
      { 
       num++; 
      } 
      if (num != 1 || !iisSettingsWithFallback.UseWindowsIntegratedAuthentication) 
      { 
       if (current.Session != null) 
       { 
        current.Session.Abandon(); 
       } 
       SPUtility.Redirect(web.ServerRelativeUrl, 0, current); 
       return; 
      } 
     } 
    } 
    if (AuthenticationMode.Forms == SPSecurity.AuthenticationMode) 
    { 
     FormsAuthentication.SignOut(); 
     if (current.Session != null) 
     { 
      current.Session.Abandon(); 
     } 
     SPUtility.Redirect(web.ServerRelativeUrl, 0, current); 
    } 
    else if (AuthenticationMode.Windows != SPSecurity.AuthenticationMode) 
    { 
     throw new SPException(); 
    } 
} 

private static string GetIPUrl(IClaimsPrincipal principal) 
{ 
    string result; 
    if (principal == null) 
    { 
     result = string.Empty; 
    } 
    else 
    { 
     string text = string.Empty; 
     try 
     { 
      string text2 = principal.Identity.Name.Split(new char[] {'|'})[1]; 
      if (SPSecurityTokenServiceManager.Local.TrustedLoginProviders[text2] != null) 
      { 
       text = SPSecurityTokenServiceManager.Local.TrustedLoginProviders[text2].ProviderUri.AbsoluteUri; 
      } 
     } 
     catch (Exception ex) 
     { 
      // log 
     } 
     result = text; 
    } 
    return result; 
} 

延伸閱讀:

+0

非常感謝你回答䳍形目。我非常感謝你的幫助。我明天開始工作就會嘗試。順便說一句,如果我要重新登錄同一頁面,我必須簽出你的方式嗎?我的意思是,我打算在用AD進行身份驗證後隱藏登錄控件,並顯示控件執行2FA操作。成功2FA後,用戶將被認證並重定向到主頁。 –

+0

不要重新登錄,您的登錄頁面和第二個身份驗證部分應該知道第一部分成功:「MFA服務器允許應用程序執行其自己的主要身份驗證,然後評估Web響應以查看主要身份驗證是成功還是失敗」 – tinamou

+0

鏈接/取消鏈接SMS設備或進行身份驗證時,2FA API將僅返回成功或異常代碼。我可能是錯的,但它是我閱讀他們的API文檔之後我所瞭解的。在那種情況下,在用AD成功驗證後簽出用戶仍然不好?我的計劃是:1)用AD認證用戶2)在認證後簽出用戶(防止用戶輸入url並訪問網站而不做2FA)3)做2FA(用他們的API方法)4)認證用戶再次給予訪問權限。我是小白。如果我錯了,請修理我。 :) –