在身份验证 ASP.CORE 期间重定向到多个页面

在我的 ASP.CORE 应用程序上,我使用了用户身份验证。


我接下来要做的是:当用户访问该站点时,如果他未在该站点上授权-向他显示主页但如果他之前已获得授权-向他显示另一个页面(在我的示例中为加密控制器页面)。但是如果他从主页导航到 /Encrypt ,程序应该将他重定向到登录页面(现在它是如何工作的)。


在配置启动时,我尝试在用户访问该站点时重定向到另一个页面 /Encrypt(默认)如果用户之前被授权 - 所有工作(他重定向到 /Encrypt 页面)但是如果他之前没有被授权,程序将重定向他到帐户控制器上的登录页面。这就是问题所在。


我注意到如果用户之前被授权(仅当之前未授权(没有身份验证 cookie)),帐户控制器不会启动,所以我无法使用帐户控制器重定向。 那么,在哪里拦截请求(或阅读它)或在哪里执行对 cookie 的授权检查?或者该怎么办?


我的启动设置:


public void ConfigureServices(IServiceCollection services)

    {

       services.AddIdentity<User, IdentityRole>()

            .AddEntityFrameworkStores<ApplicationContext>();

    ...


public void Configure(IApplicationBuilder app, IHostingEnvironment env)

    {

        ...

        app.UseAuthentication();

        app.UseMvc(routes =>

        {

            routes.MapRoute(

                name: "default",

                template: "{controller=Encrypt}/{action=Index}");

        });

    }

我的帐户控制器如下所示:


public class AccountController : Controller

{

    private readonly UserManager<User> _userManager;

    private readonly SignInManager<User> _signInManager;


    public AccountController(UserManager<User> userManager, SignInManager<User> signInManager)

    {

        _userManager = userManager;

        _signInManager = signInManager;

    }

    [HttpGet]

    public IActionResult Login(string returnUrl = null)

    {

        ViewData["UserName"] = this.GetUserName();

        return View(new LoginViewModel { ReturnUrl = returnUrl });

    }


慕姐8265434
浏览 104回答 2
2回答

收到一只叮咚

将此代码添加到您的启动以处理访问请求:&nbsp;services.ConfigureApplicationCookie(options&nbsp;=> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options.AccessDeniedPath&nbsp;=&nbsp;"/Account/Access-Denied"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options.LoginPath&nbsp;=&nbsp;"/Account/Login"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options.LogoutPath&nbsp;=&nbsp;"/Account/Signout"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options.SlidingExpiration&nbsp;=&nbsp;true; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});具体来说.AccessDeniedPath,应该通过将其附加到 Encrypt 视图来缓解您的问题...

jeck猫

我找到了部分解决方案。像这样进行配置:&nbsp; &nbsp; public void Configure(IApplicationBuilder app, IHostingEnvironment env)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; app.UseMvc(routes =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; routes.MapRoute(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: "default",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; template: "{controller=Home}/{action=Index}");&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }和这样的家庭控制器:public IActionResult Index()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (User.Identity.IsAuthenticated & !HttpContext.Session.Keys.Contains("Key"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpContext.Session.SetInt32("Key", 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return RedirectToAction("Index", "Encrypt");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return View();&nbsp; &nbsp; &nbsp; &nbsp; }但它在会话期间仅工作 1 次,因此第二次工作需要通过关闭浏览器再次启动会话。所以还是不好。如果我总是在获得授权的情况下从主页重定向 - 他将永远不会看到主页。太不好了。
打开App,查看更多内容
随时随地看视频慕课网APP