windows 错误CS0103:当前上下文中不存在名称“UserName”

ekqde3dh  于 2022-11-18  发布在  Windows
关注(0)|答案(1)|浏览(471)

我试图ASP.net根据我在Microsoft Developer Documents上找到的一些代码创建一个www.example.com身份验证系统,但我一直遇到这个错误!我以前在使用该站点的其他代码片段时遇到过这个错误,我不知道我是如何修复这些错误的。(代码片段来自here。)
我不知道该怎么做,因为我对ASP.NET没有太多的经验。
ASPX文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="WebApplication1.Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title></title>
</head>
<body style="font-family: Arial, Helvetica, sans-serif; font-size: small">
 <form id="form1" runat="server">
    <div>
       <h4 style="font-size: medium">Log In</h4>
       <hr />
       <asp:PlaceHolder runat="server" ID="LoginStatus" Visible="false">
          <p>
             <asp:Literal runat="server" ID="StatusText" />
          </p>
       </asp:PlaceHolder>
       <asp:PlaceHolder runat="server" ID="LoginForm" Visible="false">
          <div style="margin-bottom: 10px">
             <asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
             <div>
                <asp:TextBox runat="server" ID="UserName" />
             </div>
          </div>
          <div style="margin-bottom: 10px">
             <asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
             <div>
                <asp:TextBox runat="server" ID="Password" TextMode="Password" />
             </div>
          </div>
          <div style="margin-bottom: 10px">
             <div>
                <asp:Button runat="server" OnClick="SignIn" Text="Log in" />
             </div>
          </div>
       </asp:PlaceHolder>
       <asp:PlaceHolder runat="server" ID="LogoutButton" Visible="false">
          <div>
             <div>
                <asp:Button runat="server" OnClick="SignOut" Text="Log out" />
             </div>
          </div>
       </asp:PlaceHolder>
    </div>
 </form>
</body>
</html>

CS文件:

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using System;
using System.Web;
using System.Web.UI.WebControls;

namespace WebFormsIdentity
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (User.Identity.IsAuthenticated)
                {
                    StatusText.Text = string.Format("Hello {0}!!", User.Identity.GetUserName());
                    LoginStatus.Visible = true;
                    LogoutButton.Visible = true;
                }
                else
                {
                    LoginForm.Visible = true;
                }
            }
        }

        protected void SignIn(object sender, EventArgs e)
        {
            var userStore = new UserStore<IdentityUser>();
            var userManager = new UserManager<IdentityUser>(userStore);
            var user = userManager.Find(UserName.Text, Password.Text);

            if (user != null)
            {
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                StatusText.Text = "Invalid username or password.";
                LoginStatus.Visible = true;
            }
        }

        protected void SignOut(object sender, EventArgs e)
        {
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
            authenticationManager.SignOut();
            Response.Redirect("~/Login.aspx");
        }
    }
}
y1aodyip

y1aodyip1#

修正了!解决方案是将名称空间从示例代码的名称空间更改为我的名称空间。真不敢相信我之前没有看到。

相关问题