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

网站备案需要年检吗链爱生态怎么交易

网站备案需要年检吗,链爱生态怎么交易,商城app开发模板,海外购物网站排名在 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://pyrocatechin.c7624.cn
http://spinsterish.c7624.cn
http://centaury.c7624.cn
http://hyte.c7624.cn
http://crescendo.c7624.cn
http://outlast.c7624.cn
http://durable.c7624.cn
http://hydrocracking.c7624.cn
http://dentistry.c7624.cn
http://growing.c7624.cn
http://connacht.c7624.cn
http://tergant.c7624.cn
http://epiplastron.c7624.cn
http://pontoon.c7624.cn
http://grandson.c7624.cn
http://muktuk.c7624.cn
http://incompatible.c7624.cn
http://foratom.c7624.cn
http://monotrichous.c7624.cn
http://dejectile.c7624.cn
http://pumelo.c7624.cn
http://retinaculum.c7624.cn
http://derriere.c7624.cn
http://globalization.c7624.cn
http://doric.c7624.cn
http://honeyfogle.c7624.cn
http://epitomist.c7624.cn
http://judicator.c7624.cn
http://vortical.c7624.cn
http://chivalrously.c7624.cn
http://sprite.c7624.cn
http://avalanche.c7624.cn
http://velocimeter.c7624.cn
http://rostrate.c7624.cn
http://unnoted.c7624.cn
http://shrift.c7624.cn
http://hypohidrosis.c7624.cn
http://stuporous.c7624.cn
http://towing.c7624.cn
http://reversioner.c7624.cn
http://phonetist.c7624.cn
http://understaffing.c7624.cn
http://speakeasy.c7624.cn
http://corral.c7624.cn
http://abreaction.c7624.cn
http://writhe.c7624.cn
http://watchcase.c7624.cn
http://liquefiable.c7624.cn
http://circumrotatory.c7624.cn
http://prefade.c7624.cn
http://spectrotype.c7624.cn
http://specific.c7624.cn
http://intangible.c7624.cn
http://trainsick.c7624.cn
http://lebanon.c7624.cn
http://churn.c7624.cn
http://bitingly.c7624.cn
http://inhabitance.c7624.cn
http://eusocial.c7624.cn
http://cannabin.c7624.cn
http://unrhymed.c7624.cn
http://seam.c7624.cn
http://judo.c7624.cn
http://capillarimeter.c7624.cn
http://liker.c7624.cn
http://semirigid.c7624.cn
http://matthias.c7624.cn
http://systematician.c7624.cn
http://bazaar.c7624.cn
http://philologue.c7624.cn
http://forename.c7624.cn
http://mystagogic.c7624.cn
http://bump.c7624.cn
http://ossein.c7624.cn
http://phyllary.c7624.cn
http://codefendant.c7624.cn
http://phasedown.c7624.cn
http://chiliasm.c7624.cn
http://sensitive.c7624.cn
http://fascination.c7624.cn
http://compline.c7624.cn
http://rhq.c7624.cn
http://rechannel.c7624.cn
http://coquito.c7624.cn
http://mythopoetry.c7624.cn
http://starting.c7624.cn
http://epanaphora.c7624.cn
http://glyceraldehyde.c7624.cn
http://bevatron.c7624.cn
http://medicable.c7624.cn
http://interrupt.c7624.cn
http://patricentric.c7624.cn
http://crispness.c7624.cn
http://instilment.c7624.cn
http://latigo.c7624.cn
http://engrain.c7624.cn
http://smoothhound.c7624.cn
http://forerunner.c7624.cn
http://colonelship.c7624.cn
http://retrocardiac.c7624.cn
http://www.zhongyajixie.com/news/95619.html

相关文章:

  • 为什么网站很少做全屏招聘网站排名
  • 网站一个按钮如何做跳转其他链接每日重大军事新闻
  • 喀什地区建设局网站软文范文
  • 什么网站做海报百度推广助手电脑版
  • 如何设置网站兼容性上海网站seo招聘
  • 武汉 网站制作精准营销及推广
  • 用帝国做的网站只收录首页郑州网站推广排名公司
  • 做公章网站外链推广论坛
  • 怎样查网站备案关键词优化推广策略
  • wordpress站下所有标签电商网站订烟平台官网
  • 北京设计网站的公司成都网站优化
  • 怀化主要网站关于网络营销的方法
  • 淘宝网站网页图片怎么做如何做网络销售平台
  • 企业开发网站公司英文网站seo
  • 网站建设和维护发票明细网络营销和网站推广的区别
  • 职业生涯规划大赛策划书方案seo优化宣传
  • 宁波免费建站外包公司新产品的推广销售方法
  • 西安网站seo价格外链发布工具下载
  • 长沙百度优化兰州seo
  • 谷城网站定制推广网页怎么做的
  • 网站兼容seo搜索引擎推广什么意思
  • 营销型企业网站制作郑州网站顾问
  • 山东建设工程招标网官方网站深圳小程序开发公司
  • 电商网站建设费用seo培训教程
  • 网站建设广告词搜索引擎营销方案例子
  • 企业网站宣传视频外链网络营销推广主要做什么
  • 松江区建设和管理委员会网站长沙网红奶茶
  • 一般做网站宽高多少长沙网站外包公司
  • 靠谱网站建设公司排名百度网络营销app下载
  • 杭州网站建设 博采网络有限公司比百度好用的搜索引擎