简体   繁体   English

未经授权时不重定向到登录

[英]Not redirect to login when not authorized

I have an ASP.NET Core web application and I am decorating a few controller action methods with Authorize attribute. 我有一个ASP.NET Core Web应用程序,并且正在用Authorize属性修饰一些控制器动作方法。

So, when I am not logged in, it doesn't do any redirect and only shows me a blank page for that controller action. 因此,当我未登录时,它不会执行任何重定向,只会向我显示该控制器操作的空白页面。 I have gone through a couple of tutorials and they talk about Cookie authentication. 我已经看过一些教程,他们谈论了Cookie身份验证。 Below is my Configure method in Startup.cs. 下面是我在Startup.cs中的Configure方法。

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
    ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseApplicationInsightsRequestTelemetry();
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationScheme = "MainCookie",
        LoginPath = "/Login",
        AccessDeniedPath = "/Home/Forbidden/",
        AutomaticAuthenticate = true,
        AutomaticChallenge = false
    });
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationScheme = "ExternalCookie",
        AutomaticAuthenticate = false,
        AutomaticChallenge = false
    });
    app.UseGoogleAuthentication(new GoogleOptions()
    {
        ClientId = "*****",
        ClientSecret = "*****",
        SignInScheme = "ExternalCookie"
    });
    app.UseFacebookAuthentication(new FacebookOptions()
    {
        SignInScheme = "ExternalCookie",
        AppId = "AppId",
        AppSecret = "AppSecret"
    });
    app.UseLinkedInAuthentication(new LinkedInOptions()
    {
        SignInScheme = "ExternalCookie",
        ClientId = "*****",
        ClientSecret = "*****",
        ProfileScheme = LinkedInDefaults.ProfileLoadFormat.AppDefined
    });

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseApplicationInsightsExceptionTelemetry();

    app.UseStaticFiles();

    app.UseSession();
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

My action here, 我在这里的动作

[Authorize]
[HttpGet("/questions/ask", Name = "askquestions")]
public IActionResult Ask()

Looks like you're expected to see login page, if the user is not authenticated Since you configured AutomaticChallenge property to false, you won't redirect to the login page. 如果用户未通过身份验证,则似乎应该看到登录页面。由于将AutomaticChallenge属性配置为false,因此不会重定向到登录页面。 This behaviour is by design. 此行为是设计使然。 More details here 在这里更多细节

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

相关问题 用户已经登录后,如何重定向到未授权页面而不是登录页面? - How do I redirect to a not-authorized page instead of the login page when the user is already logged in? 未授权用户时出现登录对话框 - Login Dialog Appears When User is Not Authorized 在 ASP.NET Core 中禁用未授权重定向到帐户/登录 - Disable Not Authorized Redirect to Account/Login in ASP.NET Core MVC 4 在用户之前未经授权后从登录页面重定向到上一页 - MVC 4 redirect from login page to previous page after user weren´t authorized before Whatsapp登录失败未经授权 - Whatsapp Login Failed not- authorized ASP。 带有 cookie 身份验证的 Net Core 2.2:当未授权 API 仅控制器时如何避免页面重定向 - ASP. Net Core 2.2 with cookie authentication: how to avoid page redirect when not authorized for API only controllers 使用ASP.NET Core 2.2和OIDC未授权用户时,自动重定向到外部权限 - Auto redirect to external authority when user is not authorized using ASP.NET Core 2.2 and OIDC 当用户未在asp.net mvc3中授权时,重定向到另一个页面 - Redirect to another page when user is not authorized in asp.net mvc3 登录 blazor 应用程序时重定向错误 - Wrong redirect when login in blazor application 当期望值不在viewbag中时,重定向到登录页面 - Redirect to login page when expected value not in viewbag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM