当前位置: 首页 > news >正文

网站备案需要年检吗全网搜索

网站备案需要年检吗,全网搜索,江苏省城乡和住房建设厅网站,网站地图wordpress在 ASP.NET Core 应用中,基于 Cookie 的身份鉴权是一种常见的身份验证方式,特别适用于传统的 Web 应用程序。Cookie 能够在用户的浏览器中存储身份验证数据,从而在用户访问应用的不同页面时保持登录状态。 一、配置 Cookie 身份验证 首先&a…

在 ASP.NET Core 应用中,基于 Cookie 的身份鉴权是一种常见的身份验证方式,特别适用于传统的 Web 应用程序。Cookie 能够在用户的浏览器中存储身份验证数据,从而在用户访问应用的不同页面时保持登录状态。

一、配置 Cookie 身份验证

首先,在 Startup.cs 或 Program.cs 文件中配置 Cookie 身份验证。这包括设置登录路径、登出路径、Cookie 的过期时间等参数。

在 Program.cs 文件中,配置 Cookie 身份验证:

var builder = WebApplication.CreateBuilder(args);builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>{options.LoginPath = "/Account/Login";options.LogoutPath = "/Account/Logout";options.AccessDeniedPath = "/Account/AccessDenied";options.ExpireTimeSpan = TimeSpan.FromMinutes(30);options.SlidingExpiration = true;}); // 安装 Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilationbuilder.Services.AddControllersWithViews().AddRazorRuntimeCompilation(); var app = builder.Build();if (!app.Environment.IsDevelopment())
{app.UseExceptionHandler("/Home/Error");app.UseHsts();
}app.UseHttpsRedirection();
app.UseStaticFiles();app.UseRouting();app.UseAuthentication();
app.UseAuthorization();app.MapControllerRoute(name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");app.Run();

二、创建登录和登出逻辑

接下来,你需要创建处理登录和登出请求的控制器和视图。

创建 AccountController

创建一个 AccountController,用于处理登录和登出逻辑:

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using System.Threading.Tasks;publicclassAccountController : Controller
{[HttpGet]public IActionResult Login(string returnUrl = "/"){ViewData["ReturnUrl"] = returnUrl;return View();}[HttpPost][ValidateAntiForgeryToken]public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = "/"){if (ModelState.IsValid){// 验证用户名和密码if (model.Username == "admin" && model.Password == "123456"){var claims = new List<Claim>{new Claim(ClaimTypes.Name, model.Username),new Claim(ClaimTypes.Role, "Admin")  // 可以根据需要添加角色};var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);var authProperties = new AuthenticationProperties{IsPersistent = model.RememberMe,RedirectUri = returnUrl};await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,new ClaimsPrincipal(claimsIdentity),authProperties);return LocalRedirect(returnUrl);}else{ModelState.AddModelError(string.Empty, "Invalid login attempt.");}}return View(model);}[HttpPost][ValidateAntiForgeryToken]public async Task<IActionResult> Logout(){await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);return RedirectToAction("Index", "Home");}
}

创建 Login 视图

创建一个 Login.cshtml 视图,用于显示登录表单:

@model LoginViewModel<h2>Login</h2><form asp-action="Login" asp-route-returnUrl="@ViewData["ReturnUrl"]" method="post"><div asp-validation-summary="All" class="text-danger"></div><div class="form-group"><label asp-for="Username"></label><input asp-for="Username" class="form-control" /><span asp-validation-for="Username" class="text-danger"></span></div><div class="form-group"><label asp-for="Password"></label><input asp-for="Password" class="form-control" type="password" /><span asp-validation-for="Password" class="text-danger"></span></div><div class="form-group"><div class="checkbox"><label asp-for="RememberMe"><input asp-for="RememberMe" />@Html.DisplayNameFor(m => m.RememberMe)</label></div></div><button type="submit" class="btn btn-primary">Log in</button>
</form>

创建 LoginViewModel

创建一个 LoginViewModel,用于绑定登录表单的数据:

public classLoginViewModel
{[Required][Display(Name = "User name")]publicstring Username { get; set; }[Required][DataType(DataType.Password)][Display(Name = "Password")]publicstring Password { get; set; }[Display(Name = "Remember me?")]publicbool RememberMe { get; set; }
}

三、保护 API 路由

一旦配置了 Cookie 身份验证,你可以使用 [Authorize] 特性来保护你的 API 路由,确保只有经过身份验证的用户可以访问受保护的资源。

[Authorize]
public class ProtectedController : Controller
{ public IActionResult GetProtectedData(){return Ok(new { message = "This is protected data" });}
}

四、客户端请求

客户端在请求受保护的 API 时,浏览器会自动发送存储在 Cookie 中的身份验证数据。服务器会通过 Cookie 中间件验证这些数据的有效性,并允许或拒绝请求。

五、登出逻辑

用户可以通过访问登出路径来登出。登出逻辑会清除用户的 Cookie,从而结束会话。

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Logout()
{await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);return RedirectToAction("Index", "Home");
}

总结

通过以上步骤,可以在 ASP.NET Core 应用中实现基于 Cookie 的身份鉴权,确保你的应用能够安全地验证用户身份并授权访问特定资源。Cookie 的持久性和易于管理的特性使其成为传统 Web 应用中身份验证的理想选择。

http://www.15wanjia.com/news/19637.html

相关文章:

  • php旅游网站开发背景网络营销专业的就业方向
  • 做网站域名解析华夏思源培训机构官网
  • 网站备案更换市场营销方案
  • 馆陶网站建设公司北京网
  • 进行企业网站建设规划站外推广
  • 模板自助建站网站制作百度广告多少钱
  • 团购网站设计竞价推广哪里开户
  • 用帝国做的网站只收录首页竞价运营是做什么的
  • 网站开发平台软件百度推广如何代理加盟
  • 怎样查网站备案网络营销师
  • 免费做网站排名网络营销品牌推广
  • 襄阳网站建设多少钱开发软件app需要多少钱
  • 网络营销的特点主要有哪些seo咨询河北
  • 怀柔石家庄网站建设抖音推广渠道有哪些
  • 建立制度优化方案模板
  • 网站开发流程三部分开鲁网站seo站长工具
  • 庆阳市人大常委会网站建设如何规划企业网络推广方案
  • 做网站创业流程图在线磁力搜索神器
  • 北京政府网站开发友情链接网站
  • 网站文字设计百度指数网址是多少
  • 汕头网站设计制作公司怎么创建网页链接
  • 网站开发 协作平台论坛推广方案
  • 幼儿园网站建设的意义seo优化关键词排名
  • 网站建设的相关书籍sem培训班培训多少钱
  • 做网站怎么做优化公司流程制度
  • 网站甲假发头套真头发做的假发湖北荆门今日头条
  • 企业网站 seo怎么做seo技术网网
  • 做交友网站如何吸引用户注册自己如何开网站
  • 做网站是什么课搜索引擎免费下载
  • 嘉兴做外贸网站比较好的公司今天最近的新闻