当前会话用户未在Razor页面中显示。

huangapple go评论102阅读模式
英文:

current session user not displaying in razorpages

问题

以下是您提供的内容的翻译:

我有一个非常基本的Razor Pages逻辑页面。
我正在使用会话来保存当前用户的登录凭据到一个用户模型中,之后我想显示它,但即使当前用户不是null,当前用户的Name属性也为空,原因不明。

以下是代码部分:
Index.cshtml.cs

  1. public User currentUser { get; set; }
  2. [BindProperty]
  3. public string LoginUserName { get; set; }
  4. [BindProperty]
  5. public string LoginPassword { get; set; }
  6. public void OnPostLogin()
  7. {
  8. currentUser = new(LoginUserName.ToString(), LoginPassword.ToString());
  9. HttpContext.Session.SetObjectsession("currentUser", currentUser);
  10. }

Index.cshtml <br>
这里应该显示@result.Name,但它只显示检查是否进入正确if语句的"label"。

  1. &lt;form method="post" class="LoginContainer"&gt;
  2. &lt;label style="color:grey; font-size:17px; margin-left:20px;"&gt;Username&lt;/label&gt;
  3. &lt;input name="LoginUserName" class="LoginUsername" type="text"/&gt;
  4. &lt;label style="color:grey; font-size:17px; margin-left:20px;"&gt;Password&lt;/label&gt;
  5. &lt;input name="LoginPassword" class="LoginPassword" type="text" /&gt;
  6. &lt;input type="submit" class="Login" value="Login" asp-page-handler="Login"/&gt;
  7. @{
  8. var result = HttpContext.Session.GetObjectsession<User>("currentUser");
  9. }
  10. @if (result != null)
  11. {
  12. &lt;h1&gt;@result.Name&lt;/h1&gt;
  13. &lt;h1&gt;It works&lt;/h1&gt;
  14. }
  15. else
  16. {
  17. &lt;h1&gt;Current user is null&lt;/h1&gt;
  18. }

</form>

TestSession.cs

  1. public static class TestSession
  2. {
  3. //设置会话
  4. public static void SetObjectsession(this ISession session, string key, object value)
  5. {
  6. session.SetString(key, JsonConvert.SerializeObject(value));
  7. }
  8. //获取会话
  9. public static T GetObjectsession<T>(this ISession session, string key)
  10. {
  11. var value = session.GetString(key);
  12. return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
  13. }
  14. }
英文:

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

  1. public User currentUser { get; set; }
  2. [BindProperty]
  3. public string LoginUserName { get; set; }
  4. [BindProperty]
  5. public string LoginPassword { get; set; }
  6. public void OnPostLogin()
  7. {
  8. currentUser = new(LoginUserName.ToString(),LoginPassword.ToString());
  9. HttpContext.Session.SetObjectsession(&quot;currentUser&quot;, currentUser);
  10. }

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

  1. &lt;form method=&quot;post&quot; class=&quot;LoginContainer&quot;&gt;
  2. &lt;label style=&quot;color:grey; font-size:17px; margin-left:20px;&quot;&gt;Uživatelsk&#233; jm&#233;no&lt;/label&gt;
  3. &lt;input name=&quot;LoginUserName&quot; class=&quot;LoginUsername&quot; type=&quot;text&quot;/&gt;
  4. &lt;label style=&quot;color:grey; font-size:17px; margin-left:20px;&quot;&gt;Heslo&lt;/label&gt;
  5. &lt;input name=&quot;LoginPassword&quot; class=&quot;LoginPassword&quot; type=&quot;text&quot; /&gt;
  6. &lt;input type=&quot;submit&quot; class=&quot;Prihlasit&quot; value=&quot;Přihl&#225;sit&quot; asp-page-handler=&quot;Login&quot;/&gt;
  7. @{
  8. var reuslt = HttpContext.Session.GetObjectsession&lt;User&gt;(&quot;currentUser&quot;);
  9. }
  10. @if (reuslt != null)
  11. {
  12. &lt;h1&gt;@reuslt.Name&lt;/h1&gt;
  13. &lt;h1&gt;works&lt;/h1&gt;
  14. }
  15. else
  16. {
  17. &lt;h1&gt;current user is null&lt;/h1&gt;
  18. }

</form>

TestSession.cs

  1. public static class TestSession
  2. {
  3. //set session
  4. public static void SetObjectsession(this ISession session, string key, object value)
  5. {
  6. session.SetString(key, JsonConvert.SerializeObject(value));
  7. }
  8. //get session
  9. public static T GetObjectsession&lt;T&gt;(this ISession session, string key)
  10. {
  11. var value = session.GetString(key);
  12. return value == null ? default(T) : JsonConvert.DeserializeObject&lt;T&gt;(value);
  13. }
  14. }

答案1

得分: 0

UPDATE

我发现了错误,这是一个非常简单的修复。
在我的模型类中,我没有将构造函数传递的变量保存到User模型的变量中

  1. public string Name { get; set; }
  2. public string Password { get; set; }
  3. public User(string name, string password)
  4. {
  5. Name = name;
  6. Password = password;
  7. }

抱歉造成不必要的问题。

英文:

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

  1. public string Name { get; set; }
  2. public string Password { get; set; }
  3. public User(string name, string password)
  4. {
  5. Name = name;
  6. Password = password;
  7. }

Sorry for unnecesary question

huangapple
  • 本文由 发表于 2023年5月6日 18:23:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定