如何在回发和没有会话之间共享变量值

本文关键字:之间 会话 共享 共享变量 变量值 | 更新日期: 2023-09-27 18:33:27

我正在为基于共享点表单的身份验证创建自定义登录页面。

默认页具有登录控件

,登录控件具有 2 种方法:登录和授权。

登录会获得一个用户 ID,稍后我需要它来验证与外部 SMS 提供商的 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("univer.diego.s@gmail.com", cellnumber, 57);
                useridAuthy = result.UserId;
                // I need to set the user id here.
                Session["userid"] = useridAuthy;
                client.SendSms(result.UserId, true);                
            }
        }     
    }
}

因为这是 Sharepoint,所以我不确定我是否应该在 web.config 中为 Web 应用程序做一些事情,以及它是否会对 SharePoint 本身造成任何损害。多谢

如何在回发和没有会话之间共享变量值

无需修改 SharePoint 的 web.config 文件,只需将EnableSessionState="True"添加到自定义页面上的 Page 指令即可。

<%@ Page Language="C#" EnableSessionState="True" ... %>