asp.net-core – 如何使用ASP.NET Core中的JWT授权重定向到401上的登录页面
发布时间:2020-05-23 22:49:17 所属栏目:asp.Net 来源:互联网
导读:我在我的Startup.cs中有这个JWT授权配置: services.AddAuthentication(opts ={ opts.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; opts.DefaultChallengeScheme = JwtBearerDefaults.Authent
|
我在我的Startup.cs中有这个JWT授权配置: services.AddAuthentication(opts =>
{
opts.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opts.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
opts.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(opts =>
{
opts.RequireHttpsMetadata = false;
opts.SaveToken = true;
opts.TokenValidationParameters = new TokenValidationParameters()
{
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("my_secret_key")),ValidIssuer = "iss",ValidAudience = "aud",ValidateIssuerSigningKey = true,ValidateLifetime = true
};
});
我的HomeController有[Authorize]属性.因此,在访问Home / Index时,我收到了401响应,并且我看到了一个空白页面.我想重定向到我的帐户/登录页面,但我不知道该怎么做. 我读到这不应该自动重定向,因为如果它们未被授权然后你重定向它们对API调用没有意义,那么我将如何将它们带到401上的登录页面的正确方法是什么. 请记住,在这个项目中,我同时拥有带有[Authorize]属性的Web API和Action方法,因此我只需要在它是一个action方法时重定向. 解决方法您可以使用 StatusCodePages middleware.在Configure方法中添加以下内容:app.UseStatusCodePages(async context => {
var request = context.HttpContext.Request;
var response = context.HttpContext.Response;
if (response.StatusCode == (int)HttpStatusCode.Unauthorized)
// you may also check requests path to do this only for specific methods
// && request.Path.Value.StartsWith("/specificPath")
{
response.Redirect("/account/login")
}
});
这与API调用有关,它返回页面以外的数据.假设您的应用在后台调用API.将操作重定向到登录页面没有帮助,因为应用程序不知道如何在没有用户参与的情况下在后台验证自身. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – “’Microsoft.Jet.OLEDB.4.0’提供程序未在本地
- asp.net – 在资源文件中存储SQL查询是不好的做法吗?
- asp.net – System.Json.DLL在哪里?
- WF4 – 在asp.net中显示工作流图像,并突出显示活动
- 使用oAuth和ASP.NET MVC WebApi进行身份验证
- asp.net-mvc – 如何使用MVC3 Razor布局页面?
- asp.net – IIS(动态和静态)缓存,OutPutCache和浏览器缓存之
- asp.net-mvc – ASP.NET MVC验证ViewState MAC失败
- 强制ASP.NET应用程序从bin而不是GAC加载程序集
- asp.net-mvc-4 – 如何在Durandal中使用cshtml文件?
推荐文章
站长推荐
- asp.net-mvc – 在Hub,SignalR之外获取connectio
- asp.net-mvc-3 – 使用Ninject获取对象的实例
- asp.net – 使用GhostScript将PDF转换为服务器上
- asp.net – System.Web.Security.AntiXss.AntiXs
- ASP.NET实现二维码
- asp.net – 我可以在卫星装配中组合本地资源吗?
- asp.net-membership – MiniProfiler和SqlMember
- asp.net-mvc-3 – 有没有办法使用@ Html.HiddenF
- asp.net-mvc – 获取表单域的生成clientid
- asp.net-mvc-4 – ViewModels或ViewBag?
热点阅读
