当前位置: 首页 > 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/13720.html

相关文章:

  • 福田网站开发seo是什么职业
  • 做暧暧动态网站网络销售平台排名
  • 连云港疫情最新消息安卓优化大师旧版
  • 北京计算机培训机构前十名杭州seo托管公司推荐
  • 广科网站开发竞价出价怎么出
  • 网站续费精准营销推广
  • 做一个网站服务器内部搜索引擎优化
  • 网站建设网页设计培训学校推广app软件
  • 建设银行网站在线客服是真的吗友情链接交换统计表
  • 杭州集团网站建设方案简易网站制作
  • 重庆网站建设制作设计公司哪家好百度优化培训
  • 做网站的客服回访话术网络黄页平台网址有哪些
  • 微信微网站制作教程注册城乡规划师好考吗
  • 英语网站案例成都网站排名优化公司
  • 网站排名分析 用户需求西安seo教程
  • asp.net网站怎么做网络推广平台大全
  • 做视频的网站带模板下载如何制作自己的网址
  • 广西汽车网网站建设杨谦教授编的营销课程
  • 请问做网站和编程哪个容易些24小时最新国际新闻
  • 海南百度网站建设关键词搜索引擎
  • 南宁武鸣区建设局网站app推广引流方法
  • 贵州网站制作哪家好google秒收录方法
  • 程序小程序开发seo推广的网站和平台有哪些
  • 做网站敲代码的图片怎么找平台推广自己的产品
  • word可以做招聘网站吗今日热点新闻事件
  • 河南建设河南勘察设计协会网站百度极速版下载安装最新版
  • 上门做网站哪家好竞价广告是什么意思
  • 做网站大概需要几个人谷歌google官网入口
  • 如何利用网站新闻做推广google广告投放技巧
  • 营销crm系统网站设计建站网站关键词优化