gpt4 book ai didi

c# - 如何设置asp.net身份默认登录页面

转载 作者:太空宇宙 更新时间:2023-11-03 10:42:49 25 4
gpt4 key购买 nike

我开始使用新的 asp.net 标识,特别是 2.x 版本。我从一个空的网站开始,因为我喜欢从头开始,并控制进入我的应用程序的所有内容。我真的不喜欢自动魔法的东西。

我有一个目录设置为通过目录中我的 web.config 中的一些设置来保护。当未登录的用户尝试访问此 protected 目录中的页面时,用户将被重定向到:

/Account/Login?ReturnUrl=%2fUsers%2fDefault.cshtml

我不想这样。我希望用户被重定向到:

/Login.cshtml?ReturnUrl=*someurl* 

如何让重定向发生在我的 login.cshtml 文件而不是默认条目上?

我在应用程序中完成的一些事情:

我已将应用程序的 web.config 文件设置为:

<authentication mode="Forms">    
<forms loginUrl="~/Login.cshtml" timeout="3600"/>
</authentication>

在我的 startup.cs 文件中,我有:

using Microsoft.AspNet.Identity;  
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Owin;
[assembly: OwinStartup(typeof(GolfGameApp.Startup))]
namespace GolfGameApp
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Login.cshtml"),
LogoutPath = new PathString("/Logout.cshtml")
});
}
}
}

与一些 friend 交谈,看起来我必须转到目录中的 _PageStart.cshtml 文件并使用它。我已经做到了并且它按预期工作。如果有什么建议,请告诉我。我一直在努力把事情做得更好,所以建议离开。 :-)

最佳答案

在 web.config 中,您已经配置了 Forms Authentication,而在您的代码中,我看到了 OWIN 内容。如果您选择 OWIN,则必须将以下内容放入 web.config。

<authentication mode="None">    
</authentication>

您似乎还试图以错误的方式使用 MVC。
loginUrl="~/Login.cshtml" 在 MVC 模型中没有意义。您的 URL 是通过 Controller 和路由形成的。 *.chtml 文件永远不会直接公开。

ASPNET.Identity、MVC(Razor)、Owin、EF 是不同的东西。在从头开始一个项目之前,您应该确定您使用的技术和 dll 以及您将如何组合它们。

关于c# - 如何设置asp.net身份默认登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24703903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com