ASP.NET CORE MVC 5 中带有会话的登录表单

本文档详细介绍了如何在Visual Studio中创建一个ASP.NET Core MVC5项目,包括设置空模板、添加配置、创建控制器和视图,并实现简单的登录会话管理。通过实例展示了登录和注销功能的实现,以及项目的基本结构。
摘要由CSDN通过智能技术生成

创建 ASP.NET Core MVC 5 项目

在 Visual Studio 上,从“开始”中选择“创建新项目”

选择ASP.NET Core Web 应用程序



 

输入项目名称并选择新项目的位置

选择ASP.NET Core 5.0 版本并选择ASP.NET Core Empty Template。点击创建按钮完成



 

添加配置

打开Startup.cs文件并添加新配置,如下所示:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace LearnASPNETCoreMVC5
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();

            services.AddSession();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseSession();

            app.UseStaticFiles();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Demo}/{action=Index}/{id?}");
            });

        }
    }
}

创建控制器

创建名为Controllers的新文件夹。在此文件夹中,创建名为AccountController.cs的新控制器,如下所示:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace LearnASPNETCoreMVC5.Controllers
{
    [Route("account")]
    public class AccountController : Controller
    {
        [Route("")]
        [Route("index")]
        [Route("~/")]
        public IActionResult Index()
        {
            return View();
        }

        [Route("login")]
        [HttpPost]
        public IActionResult Login(string username, string password)
        {
            if (username != null && password != null && username.Equals("acc1") && password.Equals("123"))
            {
                HttpContext.Session.SetString("username", username);
                return View("Success");
            }
            else
            {
                ViewBag.error = "Invalid Account";
                return View("Index");
            }
        }

        [Route("logout")]
        [HttpGet]
        public IActionResult Logout()
        {
            HttpContext.Session.Remove("username");
            return RedirectToAction("Index");
        }
    }
}



 

创建视图

创建名为Views的新文件夹。在此文件夹中,创建名为Account的新文件夹并添加新视图,如下所示:

索引视图

Account文件夹中,创建名为Index.cshtml的新视图,如下所示:

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>

    <h3>Login Page</h3>
    @ViewBag.error
    <form method="post" asp-controller="account" asp-action="login">
        <table border="0" cellpadding="2" cellspacing="2">
            <tr>
                <td>Username</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" value="Login"></td>
            </tr>
        </table>
    </form>

</body>
</html>

成功观

Account文件夹中,创建名为Success.cshtml的新视图,如下所示:

@using Microsoft.AspNetCore.Http;

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Success</title>
</head>
<body>

    <h3>Success Page</h3>
    Welcome @Context.Session.GetString("username")
    <br>
    <a asp-controller="account" asp-action="logout">Logout</a>

</body>
</html>



 

创建 Razor 视图导入

选择Views文件夹并右键单击选择Add\New Item Menu

选择左侧的Web\ASP.NET。选择Razor View Imports项目并单击Add按钮完成

_ViewImports.cshtml文件和TagHelpers库中,如下所示:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

ASP.NET Core MVC 5 项目的结构



 

运行应用程序

使用以下 url在帐户控制器中访问索引操作: http://localhost:48982/Account/Index

输出

使用无效帐户进行测试是用户名:aa密码:123

输出

使用有效帐户进行测试是username: acc1password: 123

输出

单击成功页面中的注销链接以删除会话并再次打开登录页面

输出

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值