2016-02-05 71 views
0

我正在爲基於Sharepoint表單的身份驗證創建自定義登錄頁面。我如何在回傳和不使用會話之間共享變量值

默認頁面有一個登錄控件,登錄控件有2個方法,登錄和onauthorize。

該登錄獲取用戶ID,稍後我需要它來驗證SMS與外部短信提供商。

我試圖用會話這樣做,但我得到這個錯誤時enableSessionState設置爲true只能用

會話狀態, 無論是在配置文件或Page指令。還請 確保System.Web.SessionStateModule或自定義會話狀態 模塊包含在應用程序配置的\\ 部分中。

代碼

namespace Authentication.FBA2FA.CustomLogin.Layouts.Authentication.FBA2FA.CustomLogin 
{ 
    public partial class CustomLoginPage : FormsSignInPage 
    { 
     private string cellnumber; 
     private bool siCell; 
     AuthyClient client; 
     private string useridAuthy; 

     protected void Page_Load(object sender, EventArgs e) 
     {  
     } 

     protected void signInControl_Authenticate(object sender, AuthenticateEventArgs e) 
     { 
      SecurityToken token = null; 
      LoginControl formsLoginControl = sender as LoginControl; 

      TextBox txtphone = (TextBox)signInControl.FindControl("tbxPhone"); 
      TextBox txtSecureCode = (TextBox)signInControl.FindControl("tbxSecureCode"); 

      client = GetAuthyClient(); 

    // I need to get the user id here. 
      var tokenresult = client.VerifyToken(Session["userid"].ToString(), txtSecureCode.Text); 
      if (tokenresult.Status != AuthyStatus.Success) 
      { 
       siCell = true; 
      } 
      else 
      { 
       siCell = false; 
      } 

      //bSendSms.Click += bSendSms_Click; 

      if (null != (token = GetSecurityToken(formsLoginControl))) 
      { 
       if (siCell) 
       { 
        EstablishSessionWithToken(token); 
        e.Authenticated = true; 
        base.RedirectToSuccessUrl(); 
       } 
       else 
       { 
        e.Authenticated = false; 
       }       
      } 
     } 
     //private void bSendSms_Click(object sender, EventArgs e) 
     //{    
     //} 
     private SPIisSettings IisSettings 
     { 
      get 
      {  
       SPWebApplication webApp = SPWebApplication.Lookup(new Uri(SPContext.Current.Web.Url)); 
       SPIisSettings settings = webApp.IisSettings[SPUrlZone.Default]; 
       return settings; 
      } 
     } 

     private SecurityToken GetSecurityToken(LoginControl formsLoginControl) 
     { 
      SecurityToken token = null; 
      SPIisSettings iisSettings = IisSettings; 
      Uri appliesTo = base.AppliesTo; 

      if (string.IsNullOrEmpty(formsLoginControl.UserName) || string.IsNullOrEmpty(formsLoginControl.Password)) 
       return null; 

      SPFormsAuthenticationProvider authProvider = iisSettings.FormsClaimsAuthenticationProvider; 
      token = SPSecurityContext.SecurityTokenForFormsAuthentication(
       appliesTo, 
       authProvider.MembershipProvider, 
       authProvider.RoleProvider, 
       formsLoginControl.UserName, 
       formsLoginControl.Password); 

      return token;   
     } 

     protected void btnSms_Click(object sender, EventArgs e) 
     { 
      TextBox txtSecureCode = (TextBox)signInControl.FindControl("tbxSecureCode"); 
      TextBox txtphone = (TextBox)signInControl.FindControl("tbxPhone"); 
      int n; 
      bool isNumeric = int.TryParse(txtphone.Text, out n); 

      if (string.IsNullOrEmpty(txtphone.Text) && !isNumeric) 
      { 
       ClaimsFormsPageMessage.Text = "Please insert a cellphone number"; 
      } 
      else 
      { 
       cellnumber = txtphone.Text; 
       client = GetAuthyClient(); 
       var result = client.RegisterUser("[email protected]", cellnumber, 57); 
       useridAuthy = result.UserId; 
       // I need to set the user id here. 
       Session["userid"] = useridAuthy; 

       client.SendSms(result.UserId, true);     
      } 
     }  
    } 
} 

因爲這是共享點,我不知道我是否應該在web.config的Web應用程序做一些事情,如果它會做任何損害到SharePoint本身。 非常感謝

回答

1

除了修改SharePoint的web.config文件外,您還可以將EnableSessionState="True"添加到自定義頁面上的Page指令中。

<%@ Page Language="C#" EnableSessionState="True" ... %> 
相關問題