Asp.Net Core中完成拒绝访问功能
很多时候如果用户没有某个菜单的操作权限的话在页面是不应该显示出来的。
@if (SignInManager.IsSignedIn(User) && User.IsInRole("Admin"))
{
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
管理
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" asp-controller="Admin"
asp-action="ListUsers">用户列表</a>
<a class="dropdown-item" asp-controller="Admin"
asp-action="ListRoles">角色列表</a>
</div>
</li>
}
如果通过Url来访问时,默认会跳转到一个Account/AccessDenied的拒绝页面,所以我们需要在Account控制器中定义一个AccessDenied方法,添加其视图。
[HttpGet]
[AllowAnonymous]
public IActionResult AccessDenied()
{
return View();
}
<div class="text-center">
<h1 class="text-danger">拒绝访问</h1>
<h6 class="text-danger">您没有查看此资源的权限</h6>
<img src="~/images/noaccess.png" style="height:300px; width:300px" />
</div>
当然我们可以自定义拒绝跳转页面,那就是在startup中添加 options.AccessDeniedPath ,如下:
services.ConfigureApplicationCookie(options =>
{
options.AccessDeniedPath = "/Identity/Account/AccessDenied";
//options.Cookie.Name = "YourAppCookieName";
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes();
//options.LoginPath = "/Identity/Account/Login";
// ReturnUrlParameter requires
//using Microsoft.AspNetCore.Authentication.Cookies;
options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
options.SlidingExpiration = true;
});
Asp.Net Core中完成拒绝访问功能的更多相关文章
- 为什么我的会话状态在ASP.NET Core中不工作了?
原文:Why isn't my session state working in ASP.NET Core? Session state, GDPR, and non-essential cookie ...
- Asp.Net Core中利用Seq组件展示结构化日志功能
在一次.Net Core小项目的开发中,掌握的不够深入,对日志记录并没有好好利用,以至于一出现异常问题,都得跑动服务器上查看,那时一度怀疑自己肯定没学好,不然这一块日志不可能需要自己扒服务器日志来查看 ...
- ASP.NET Core 中文文档 第三章 原理(15)请求功能
作者:Steve Smith 翻译:谢炀(kiler398) 校对:姚阿勇(Dr.Yao).孟帅洋(书缘) 涉及到如何处理 HTTP 请求以及响应的独立 Web 服务器功能已经被分解成独立的接口,这些 ...
- [译]在Asp.Net Core 中使用外部登陆(google、微博...)
原文出自Rui Figueiredo的博文<External Login Providers in ASP.NET Core> 摘要:本文主要介绍了使用外部登陆提供程序登陆的流程,以及身份 ...
- asp.net Core 中AuthorizationHandler 实现自定义授权
前言 ASP.NET Core 中 继承的是AuthorizationHandler ,而ASP.NET Framework 中继承的是AuthorizeAttribute. 它们都是用过重写里面的方 ...
- 【译】在Asp.Net Core 中使用外部登陆(google、微博...)
原文出自Rui Figueiredo的博文<External Login Providers in ASP.NET Core> (本文很长) 摘要:本文主要介绍了使用外部登陆提供程序登陆的 ...
- ASP.NET Core 中的那些认证中间件及一些重要知识点
前言 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系列(一,二,三)奠定一下基础. 有关于 Authentication 的知识太广,所以本篇介绍几个在 A ...
- 在ASP.NET Core中使用百度在线编辑器UEditor
在ASP.NET Core中使用百度在线编辑器UEditor 0x00 起因 最近需要一个在线编辑器,之前听人说过百度的UEditor不错,去官网下了一个.不过服务端只有ASP.NET版的,如果是为了 ...
- ASP.NET Core 中文文档 第三章 原理(1)应用程序启动
原文:Application Startup 作者:Steve Smith 翻译:刘怡(AlexLEWIS) 校对:谢炀(kiler398).许登洋(Seay) ASP.NET Core 为你的应用程 ...
随机推荐
- Firefox设置谷粉搜搜为默认搜索引擎的方法
原文转自:http://www.gfsoso.org/119/ 作者: cjx 分类: 谷粉专题 发布时间: 2014-07-09 23:10 ė 668条评论 如果使用Firefox的朋友希 ...
- 贴一段Matlab代码
% reduce leading zeros with rx ind3= find(rx~=0, 1, 'first'); if (isempty(ind3)) rx= gf(0, m, f0); e ...
- 1066 Root of AVL Tree (25)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- SignalR要求_转自:https://www.cnblogs.com/humble/p/3855137.html
SignalR 服务端组件可以被部署在诸多的服务器配置中,本节描述了它所支持的操作系统版本,.NET framework,IIS.以及其他组件 二.支持的服务器操作系统 SignalR服务端组件可以被 ...
- Mybatis笔记(二)
目录 MyBatis 逆向工程 MyBatis Generator 使用 分页插件 1.下载分页插件 2.配置分页插件 3.使用分页插件 SSM整合(spring与springMVC) 1.创建web ...
- JAVA日志工具类
package com.ming.util; import java.io.File; import java.io.FileWriter; import java.io.IOException; i ...
- Python Django使用HttpResponse返回图片并显示
views.py def read_img(request): """ : 读取图片 :param request: :return: """ ...
- face morhper
图像变形背后的想法很简单.给定两个图像,我们想通过将图像和混合来创建中间图像.图像的混合和由参数控制的是在0和1之间().当为0时,变形看起来像,而当为1 时,变形看起来像.天真的,您可以在每个像素上 ...
- https://zhuanlan.zhihu.com/p/32553477
科普:QUIC协议原理分析
- Android 调用.Net WCF服务 .
本来以为在java平台上用axis2生成了客户端代理类然后移植到Android平台上就好了.没想到在移植过程中出现了很多问题.说明JVM和android的DVM差距还是很大的. JVM执行的是clas ...