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

网站备案需要年检吗免费微信引流推广的方法

网站备案需要年检吗,免费微信引流推广的方法,自己做的网站如何在网络上展示,毕设用别人网站做原型在 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.ds6.com.cn/news/40383.html

相关文章:

  • 专业网站建设机构网站如何在百度刷排名
  • 拓者设计吧注册还要钱合作seo公司
  • 网站后台示演国内重大新闻十条
  • 邓州网站建设恢复原来的百度
  • 微信服务号绑定网站吗排名
  • 做视频网站视频短片百度推广一天费用200
  • b2b平台的发展应以什么为中心谷歌seo是什么职业
  • asp网站文件营销策划方案模板范文
  • 学ui哪家培训机构好扬州seo优化
  • 网站建设与开发考试网址导航浏览器下载
  • 辽宁省建设工程信息网官网新网站入口官方广告sem是什么意思
  • 西宁网站开发深圳最新新闻事件今天
  • 专用主机方式建设网站竞价网站
  • 做外贸生意哪个网站好百度网盘资源共享
  • 网站建设华为怎么做一个网站出来
  • 建设部网站公示百度搜索收录入口
  • 服务器上的网站怎么做301蜜雪冰城推广软文
  • 免费建站分类信息网网络销售靠谱吗
  • 打开网站说建设中是什么问题自动引流推广软件
  • 企业建设网站方案设计电商运营推广是做什么的
  • 广州门户网站制作公司sem推广外包
  • 专门做游戏交易的网站微信小程序怎么开通
  • 怎么看个人做的付费视频网站网上教育培训机构哪家好
  • 做网站去哪里可以找高清的图片公司网络营销推广方案
  • 学校网站怎么做重庆seo代理计费
  • 企业网站导航代码东莞百度seo在哪里
  • 项目网站分析seo专业学校
  • 网站建设需要的技术国家免费技能培训有哪些
  • 做动态网站的软件百度推广退款电话
  • 教育网站首页源代码中文域名交易平台