简体   繁体   English

渲染默认视图后,AspNet MVC使我弹回登录

[英]AspNet MVC bounces me back to login AFTER rendering default view

After a successful login using Aspnet Identity (SignInStatus.Success), MVC goes through the entire code of the default action and view, but then mysteriously bounces me back to the login page. 使用Aspnet身份(SignInStatus.Success)成功登录后,MVC会遍历默认操作和视图的整个代码,但随后神秘地将我弹回登录页面。

Strangely, this only happens with one of my user roles. 奇怪的是,这仅在我的一个用户角色中发生。 I have two roles, admin and editor . 我有两个角色, admineditor admin works fine. 管理员工作正常。 editor USED to work. 编辑曾用于工作。

Here's my default view authorization code... 这是我的默认查看授权代码..​​.

[Authorize(Roles = "Admin, Editor")]
public ActionResult Index()
{ ...

If anyone could suggest what the problem could be, or how I could debug it, that would be much appreciated. 如果有人能提出问题可能是什么,或者我如何调试它,那将不胜感激。 I've hit a brick wall. 我撞到了砖墙。 The debugger steps without errors right through the action code, the view code, and the layout code right to the last line of Razor in the layout, which is 调试器直接在布局中Razor的最后一行上的操作代码,视图代码和布局代码中正确执行步骤,即

@RenderSection("Scripts", required: false) 

I've tried it on two different browsers. 我已经在两种不同的浏览器上进行过尝试。 I thought it might be an issue with the anti-forgery token. 我认为这可能与防伪令牌有关。 I did get the following message once... "The provided anti-forgery token was meant for a different claims-based user than the current user". 我确实收到了以下消息……“提供的防伪令牌是针对与当前用户不同的基于索赔的用户的”。

But I think this was just a glitch from so many login attempts during debugging. 但是我认为这只是调试期间如此多次登录尝试的故障。

I'm running... VS 2015 .NET Framework 4.5.2 Microsoft.AspNet.Identity.Core, Microsoft.AspNet.Identity.EntityFramework, Microsoft.AspNet.Identity.Owin all version 2.2.1 我正在运行... VS 2015 .NET Framework 4.5.2 Microsoft.AspNet.Identity.Core,Microsoft.AspNet.Identity.EntityFramework,Microsoft.AspNet.Identity.Owin所有版本2.2.1

Any help would be much appreciated. 任何帮助将非常感激。

Even though the controller and the view authorized the Editor role, my layout was calling an action in another controller using 即使控制器和视图授权了“ 编辑者”角色,我的布局仍在使用

@Html.Action(...

That controller had 那个控制器有

[Authorize(Roles = "Admin")]

above its class definition. 高于其类定义。

Hence DotNet decided (even though it had continued through the rendering code for the entire view and layout) to bounce back to the login page. 因此,DotNet决定(即使它已继续遍历整个视图和布局的呈现代码)仍反弹回登录页面。

Thanks to @MarkHomer. 感谢@MarkHomer。 His comment gave me the idea to look for this. 他的评论使我有了寻找这个想法的想法。

I fixed the problem by putting [Authorize(Roles = "Admin,Editor")] above the class definition and then setting [Authorize(Roles = "Admin")] for individual actions that only needed admin access. 通过将[Authorize(Roles =“ Admin,Editor”)]放在类定义上方,然后为仅需要管理员访问权限的单个操作设置[Authorize(Roles =“ Admin”)],即可解决此问题。

You must be redirecting the user to a RedirectURL post successful login. 成功登录后,您必须将用户重定向到RedirectURL。 Have you checked that your logged-in user persists during this redirection or once it gets to the redirected URL. 您是否检查过登录用户在此重定向期间或到达重定向URL后是否仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM