2017-05-26 80 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)认证用户再次给予访问权限。我是小白。如果我错了,请修理我。 :) –