首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >会话在site.master页面中返回null

会话在site.master页面中返回null
EN

Stack Overflow用户
提问于 2017-04-12 13:18:24
回答 1查看 519关注 0票数 1

我试图将一个值从asp.net登录控件传递到一个完全不同的site.master页面。这是我的login.aspx.cs页面-

代码语言:javascript
复制
    protected void LoginUser_OnAuthenticate(object sender, AuthenticateEventArgs e)
    {
        Session["username"] = LoginUser.UserName;
        Security.AuthenticateUser(LoginUser.UserName, LoginUser.Password, LoginUser.RememberMeSet);
    }

代码的这一部分从login.aspx页面接收值-

代码语言:javascript
复制
<asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false" OnAuthenticate="LoginUser_OnAuthenticate">
    <div class="form-group">
         <label>Username</label>
         <asp:TextBox ID="UserName" runat="server" AutoCompleteType="None" CssClass="textEntry ltr-dir"></asp:TextBox>
    </div>
    <div class="form-group">
         <label>Password</label>
         <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry ltr-dir" TextMode="Password"></asp:TextBox>
   </div>
</asp:Login>

这是我的site.master页面-

代码语言:javascript
复制
var username = Session["username"].ToString();

var settings = ConfigurationManager.ConnectionStrings["BlogEngine"].ConnectionString;
SqlConnection conn = new SqlConnection(settings);

每当我调试时,Var username都会得到一个空值。而在login.aspx.cs页面中,它将用户名的值传递给会话。

请问我该如何解决这个问题?

注:Security.AuthenticateUer()法-

代码语言:javascript
复制
public static bool AuthenticateUser(string username, string password, bool rememberMe)
    {
        string un = (username ?? string.Empty).Trim();
        //string pw = (password ?? string.Empty).Trim();

        if (!string.IsNullOrWhiteSpace(un))
        {
            var user = Membership.GetUser(un);
            string res = Convert.ToString(user);
            bool isValidated = Membership.ValidateUser(res, DEFAULT_PASSWORD);
            if (isValidated)
            {
                if (BlogConfig.SingleSignOn)
                {
                    FormsAuthentication.SetAuthCookie(un, rememberMe);
                    return true;
                }

                HttpContext context = HttpContext.Current;
                DateTime expirationDate = DateTime.Now.Add(FormsAuthentication.Timeout);

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                    1,
                    un,
                    DateTime.Now,
                    expirationDate,
                    rememberMe,
                    $"{SecurityValidationKey}{AUTH_TKT_USERDATA_DELIMITER}{Blog.CurrentInstance.Id}",
                    FormsAuthentication.FormsCookiePath
                );

                string encryptedTicket = FormsAuthentication.Encrypt(ticket);

                // setting a custom cookie name based on the current blog instance.
                // if !rememberMe, set expires to DateTime.MinValue which makes the
                // cookie a browser-session cookie expiring when the browser is closed.
                System.Web.HttpCookie cookie = new System.Web.HttpCookie(FormsAuthCookieName, encryptedTicket);
                cookie.Expires = rememberMe ? expirationDate : DateTime.MinValue;
                cookie.HttpOnly = true;
                context.Response.Cookies.Set(cookie);

                string returnUrl = context.Request.QueryString["returnUrl"];
                Console.WriteLine("Redirect To This URL :" + returnUrl);

                // ignore Return URLs not beginning with a forward slash, such as remote sites.
                if (string.IsNullOrWhiteSpace(returnUrl) || !returnUrl.StartsWith("/"))
                    returnUrl = null;

                if (!string.IsNullOrWhiteSpace(returnUrl))
                {
                    context.Response.Redirect(returnUrl);
                }
                else
                {
                    if (IsReportUser(un))
                    {
                        var reportPage = "";
                        context.Response.Redirect(reportPage);
                    };

                     context.Response.Redirect(Utils.RelativeWebRoot); 
                }

                return true;
            }
        }
        return false;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-12 13:41:34

由于您正在使用FormAuthentication,,所以用户名是真实存储在原则对象中的。你可以像这样检索用户名-

代码语言:javascript
复制
var username = User.Identity.Name;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43370703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档