英文:
current session user not displaying in razorpages
问题
以下是您提供的内容的翻译:
我有一个非常基本的Razor Pages逻辑页面。
我正在使用会话来保存当前用户的登录凭据到一个用户模型中,之后我想显示它,但即使当前用户不是null,当前用户的Name属性也为空,原因不明。
以下是代码部分:
Index.cshtml.cs
public User currentUser { get; set; }
[BindProperty]
public string LoginUserName { get; set; }
[BindProperty]
public string LoginPassword { get; set; }
public void OnPostLogin()
{
currentUser = new(LoginUserName.ToString(), LoginPassword.ToString());
HttpContext.Session.SetObjectsession("currentUser", currentUser);
}
Index.cshtml <br>
这里应该显示@result.Name,但它只显示检查是否进入正确if语句的"label"。
<form method="post" class="LoginContainer">
<label style="color:grey; font-size:17px; margin-left:20px;">Username</label>
<input name="LoginUserName" class="LoginUsername" type="text"/>
<label style="color:grey; font-size:17px; margin-left:20px;">Password</label>
<input name="LoginPassword" class="LoginPassword" type="text" />
<input type="submit" class="Login" value="Login" asp-page-handler="Login"/>
@{
var result = HttpContext.Session.GetObjectsession<User>("currentUser");
}
@if (result != null)
{
<h1>@result.Name</h1>
<h1>It works</h1>
}
else
{
<h1>Current user is null</h1>
}
</form>
TestSession.cs
public static class TestSession
{
//设置会话
public static void SetObjectsession(this ISession session, string key, object value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
//获取会话
public static T GetObjectsession<T>(this ISession session, string key)
{
var value = session.GetString(key);
return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
}
}
英文:
i have a super basic logic page in razorpages
I am using sessions to save current user login credentials into a user model, i wanna display it after, but even though the currentuser isnt null, the currentuser.Name is empty for some reason
here is the code:
Index.cshtml.cs
public User currentUser { get; set; }
[BindProperty]
public string LoginUserName { get; set; }
[BindProperty]
public string LoginPassword { get; set; }
public void OnPostLogin()
{
currentUser = new(LoginUserName.ToString(),LoginPassword.ToString());
HttpContext.Session.SetObjectsession("currentUser", currentUser);
}
Index.cshtml <br>
Here it should display @reuslt.Name, but it displays just the "works" label that is checking if it goes into the right if
<form method="post" class="LoginContainer">
<label style="color:grey; font-size:17px; margin-left:20px;">Uživatelské jméno</label>
<input name="LoginUserName" class="LoginUsername" type="text"/>
<label style="color:grey; font-size:17px; margin-left:20px;">Heslo</label>
<input name="LoginPassword" class="LoginPassword" type="text" />
<input type="submit" class="Prihlasit" value="Přihlásit" asp-page-handler="Login"/>
@{
var reuslt = HttpContext.Session.GetObjectsession<User>("currentUser");
}
@if (reuslt != null)
{
<h1>@reuslt.Name</h1>
<h1>works</h1>
}
else
{
<h1>current user is null</h1>
}
</form>
TestSession.cs
public static class TestSession
{
//set session
public static void SetObjectsession(this ISession session, string key, object value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
//get session
public static T GetObjectsession<T>(this ISession session, string key)
{
var value = session.GetString(key);
return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
}
}
答案1
得分: 0
UPDATE
我发现了错误,这是一个非常简单的修复。
在我的模型类中,我没有将构造函数传递的变量保存到User模型的变量中
public string Name { get; set; }
public string Password { get; set; }
public User(string name, string password)
{
Name = name;
Password = password;
}
抱歉造成不必要的问题。
英文:
UPDATE
i found the mistake, it was a very easy fix.
In my model class i haven't saved the constructor sent variables into the User model variables
public string Name { get; set; }
public string Password { get; set; }
public User(string name, string password)
{
Name = name;
Password = password;
}
Sorry for unnecesary question
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论