<tfoot id='2YdFT'></tfoot>

<small id='2YdFT'></small><noframes id='2YdFT'>

  • <i id='2YdFT'><tr id='2YdFT'><dt id='2YdFT'><q id='2YdFT'><span id='2YdFT'><b id='2YdFT'><form id='2YdFT'><ins id='2YdFT'></ins><ul id='2YdFT'></ul><sub id='2YdFT'></sub></form><legend id='2YdFT'></legend><bdo id='2YdFT'><pre id='2YdFT'><center id='2YdFT'></center></pre></bdo></b><th id='2YdFT'></th></span></q></dt></tr></i><div id='2YdFT'><tfoot id='2YdFT'></tfoot><dl id='2YdFT'><fieldset id='2YdFT'></fieldset></dl></div>

    <legend id='2YdFT'><style id='2YdFT'><dir id='2YdFT'><q id='2YdFT'></q></dir></style></legend>

        • <bdo id='2YdFT'></bdo><ul id='2YdFT'></ul>

        ASP.NET 核心,更改未经授权的默认重定向

        ASP.NET core, change default redirect for unauthorized(ASP.NET 核心,更改未经授权的默认重定向)
        • <bdo id='slw2b'></bdo><ul id='slw2b'></ul>

            <small id='slw2b'></small><noframes id='slw2b'>

            <i id='slw2b'><tr id='slw2b'><dt id='slw2b'><q id='slw2b'><span id='slw2b'><b id='slw2b'><form id='slw2b'><ins id='slw2b'></ins><ul id='slw2b'></ul><sub id='slw2b'></sub></form><legend id='slw2b'></legend><bdo id='slw2b'><pre id='slw2b'><center id='slw2b'></center></pre></bdo></b><th id='slw2b'></th></span></q></dt></tr></i><div id='slw2b'><tfoot id='slw2b'></tfoot><dl id='slw2b'><fieldset id='slw2b'></fieldset></dl></div>

                <tbody id='slw2b'></tbody>

              • <legend id='slw2b'><style id='slw2b'><dir id='slw2b'><q id='slw2b'></q></dir></style></legend>

                  <tfoot id='slw2b'></tfoot>
                  本文介绍了ASP.NET 核心,更改未经授权的默认重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试重定向到 ASP.NET MVC6 中的不同登录 url

                  我的账户控制器登录方法有一个Route属性来改变url.

                  [HttpGet][允许匿名][路线(登录")]公共 IActionResult 登录(字符串 returnUrl = null){this.ViewData["ReturnUrl"] = returnUrl;返回 this.View();}

                  <块引用>

                  当尝试访问未经授权的页面时,我被重定向到无效的 url,它应该只是 /login 但我得到了http://localhost/Account/Login?ReturnUrl=%2Fhome%2Findex

                  我已经配置了cookie认证路径如下:

                  services.Configure(opt =>{opt.LoginPath = new PathString("/login");});

                  我添加了一个默认过滤器,以确保默认情况下所有 url 都需要身份验证.

                  services.AddMvc(选项=>{options.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));});

                  我已经检查了 url /login 确实加载了登录页面,而 /account/login 没有,如预期的那样.

                  我已将路线保持原样(除了更改默认控制器和操作)

                  app.UseMvc(routes =>{路线.MapRoute(名称:默认",模板:{controller=Site}/{action=Site}/{id?}");});

                  解决方案

                  如果勾选 UseIdentity 扩展方法 here 你会注意到它使用的是 IdentityOptions 而不是 CookieAuthenticationOptions,所以相反,您必须配置 IdentityOptions:

                  services.Configure(opt =>{opt.Cookies.ApplicationCookie.LoginPath = new PathString("/login");});

                  编辑

                  对于 asp.net core 2.0:身份 cookie 选项不再是 IdentityOptions 的一部分.检查 mxmissile 的答案.

                  I am attempting to redirect to a different login url in ASP.NET MVC6

                  My account controller login method has a Route attribute to change the url.

                  [HttpGet]
                  [AllowAnonymous]
                  [Route("login")]
                  public IActionResult Login(string returnUrl = null)
                  {
                      this.ViewData["ReturnUrl"] = returnUrl;
                      return this.View();
                  }
                  

                  When attempting to access an unathorized page, I am redirected to the invalid url, it should just be /login but instead I get http://localhost/Account/Login?ReturnUrl=%2Fhome%2Findex

                  I have configured the cookie authentication path as follows:

                  services.Configure<CookieAuthenticationOptions>(opt =>
                  {
                      opt.LoginPath = new PathString("/login");
                  });
                  

                  I have added a default filter, to ensure that all urls require authentication by default.

                  services.AddMvc(
                      options =>
                      {
                          options.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
                      });
                  

                  I have checked that the url /login does in fact load the login page, whilst /account/login does not, as expected.

                  edit: I have left the routes as is, (apart from changing the default controller and action)

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

                  解决方案

                  If you check UseIdentity extension method here you will notice that it is using IdentityOptions not CookieAuthenticationOptions, so instead you must configure IdentityOptions:

                  services.Configure<IdentityOptions>(opt =>
                  {
                      opt.Cookies.ApplicationCookie.LoginPath = new PathString("/login");
                  });
                  

                  Edit

                  For asp.net core 2.0: Identity cookie options are no longer part of IdentityOptions. Check mxmissile's answer.

                  这篇关于ASP.NET 核心,更改未经授权的默认重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)
                  <i id='JX8EN'><tr id='JX8EN'><dt id='JX8EN'><q id='JX8EN'><span id='JX8EN'><b id='JX8EN'><form id='JX8EN'><ins id='JX8EN'></ins><ul id='JX8EN'></ul><sub id='JX8EN'></sub></form><legend id='JX8EN'></legend><bdo id='JX8EN'><pre id='JX8EN'><center id='JX8EN'></center></pre></bdo></b><th id='JX8EN'></th></span></q></dt></tr></i><div id='JX8EN'><tfoot id='JX8EN'></tfoot><dl id='JX8EN'><fieldset id='JX8EN'></fieldset></dl></div>

                  1. <small id='JX8EN'></small><noframes id='JX8EN'>

                  2. <tfoot id='JX8EN'></tfoot>
                      <legend id='JX8EN'><style id='JX8EN'><dir id='JX8EN'><q id='JX8EN'></q></dir></style></legend>
                          <bdo id='JX8EN'></bdo><ul id='JX8EN'></ul>
                            <tbody id='JX8EN'></tbody>