gpt4 book ai didi

c# - 有没有关于在没有默认 ASP.Net 成员提供程序的情况下创建 MVC3 登录系统的教程?

转载 作者:行者123 更新时间:2023-11-30 15:43:10 25 4
gpt4 key购买 nike

我正在寻找有关在不使用默认 ASP.Net 成员提供程序的情况下为 ASP.Net MVC3 创建登录系统的教程或小型介绍性指南。

我已经使用 Internet 应用程序模板创建了一个新的 MVC3 项目,这里是 _LogOnPartial.cshtml 的内容:

@if(Request.IsAuthenticated) {
<text>Welcome <strong>@User.Identity.Name</strong>!
[ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text>
}
else {
@:[ @Html.ActionLink("Log On", "LogOn", "Account") ]
}

@User.Identity.Name 是成员(member)提供者的一部分吗?我想阅读更多有关 ASP.Net MVC3 成员资格的信息,因为我已经有了一个包含用户凭据的数据库,所以我不需要 prapackaged。

谢谢!

最佳答案

在 ASP.NET MVC 中实现自定义身份验证代码实际上相当容易。

在您的 Controller 的 LogOn 方法中,您需要在验证用户凭据后调用 FormsAuthentication 提供程序。

public ActionResult LogOn(LogOnModel model)
{
//Handle custom authorization then call the FormsAuthentication provider

FormsAuthentication.SetAuthCookie(/*user name*/, true);

//Return view
}

调用此方法后,将填充 User.Identity.Name,您可以在 Controller 或 Controller 方法上使用 AuthorizeAttribute

在 Controller 的 LogOff 方法中,您将再次调用 FormsAuthentication 提供程序

public ActionResult LogOff()
{
FormsAuthentication.SignOut();

//Return view
}

关于c# - 有没有关于在没有默认 ASP.Net 成员提供程序的情况下创建 MVC3 登录系统的教程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6950558/

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