我正在尝试将asp.net登录控件中的值传递到完全不同的site.master页面。这是我的login.aspx.cs页面 -
protected void LoginUser_OnAuthenticate(object sender, AuthenticateEventArgs e)
{
Session["username"] = LoginUser.UserName;
Security.AuthenticateUser(LoginUser.UserName, LoginUser.Password, LoginUser.RememberMeSet);
}
这部分代码从login.aspx页面接收值 -
<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页面 -
var username = Session["username"].ToString();
var settings = ConfigurationManager.ConnectionStrings["BlogEngine"].ConnectionString;
SqlConnection conn = new SqlConnection(settings);
Var username在我调试的任何时候都会得到一个空值。而在login.aspx.cs
页面中,它会将用户名的值传递给会话。
请问我该如何解决?
NB:Security.AuthenticateUer()
方法 -
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;
}
答案 0 :(得分:1)
由于您使用的是 FormAuthentication ,因此用户名实际存储在 原则 对象中。您可以像这样检索用户名 -
var username = User.Identity.Name;