如果用户未登录.NET Core,如何重定向用户?

时间:2018-06-15 17:55:24

标签: c# .net

我正在使用Razor Pages在.NET Core制作一个Web应用程序。

如果用户未登录到登录页面,我想重定向用户。如果他们登录 - 系统应该将用户重定向到它的第一个请求的链接/原始链接。

这是我当前的Configure - 方法:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider provider)
{

    ...

     app.Use(async (ctx, next) =>
     {
        await next();
        if(ctx.Response.StatusCode == 404 && !ctx.Response.HasStarted)
          {
              //Re-execute the request so the user gets the error page
              string originalPath = ctx.Request.Path.Value;
              ctx.Items["originalPath"] = originalPath;
              ctx.Request.Path = "/error";
              await next();
          }
     });
     app.UseStaticFiles();

     app.UseAuthentication();

     app.UseMvc();

}

因此,如果用户请求/仪表板/设置/帐户/密码,例如他没有登录,我想首先将他重定向到登录页面,在他提交后,他将被重定向到该链接/ Dashboard。

我不确定如何调整此脚本(source

修改

// In my login
if (result.Succeeded)
{
 _logger.LogInformation("User logged in.");

  if(!string.IsNullOrEmpty(returnUrl))
    return Redirect(returnUrl);

  return RedirectToPage("/Dashboard/Index");
}

我认为问题出在这里。我的ReturnUrl是null。所以它总是重定向到主页。

1 个答案:

答案 0 :(得分:1)

我为cookie身份验证做了什么

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        primary: true,
        appBar: AppBar(
        elevation: 4.0,
        backgroundColor: Color(0xfff8f8f8),
        title: Center(child: titleLogo,),
        ),
  //------------ PROBLEM BELOW ----------------
        body: new Column(
          children: <Widget>[
            titleLogo,   //Image object
            TheGridView().build()  //Returns a GridView object
            ],
      ),
  ),
);