C# 如何使用 ASP.NET MVC4 登录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13241319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:53:30  来源:igfitidea点击:

How to login using ASP.NET MVC4?

c#asp.net-mvcloginasp.net-mvc-4

提问by Kalina

Here are some bits of code from my attempt. It does not work; after "logging in", the user isn't redirected to the home/index page, but the login page just reloads.

这是我尝试中的一些代码。这是行不通的; “登录”后,用户不会被重定向到主页/索引页面,但登录页面只是重新加载。

Web.config:

网页配置:

<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" />
</authentication>

...

<defaultDocument>
  <files>
    <add value="~/Account/Login"/>
  </files>
</defaultDocument>

Controllers/AccountController.cs:

控制器/AccountController.cs:

public class AccountController : Controller
{
    //
    // GET: /Account/Login
    public ActionResult Login()
    {
        return View();
    }
}

Views/Account/Login.aspx:

视图/帐户/Login.aspx:

<asp:Login ID="login" runat="server" DestinationPageUrl="~/Views/Home/Index.aspx">
    ...
</asp:Login>

采纳答案by Yasser Shaikh

Are you using the internet template of ASP.NET MVC 4 ? if not I suggest you create a new ASP.NET MVC project with internet template, open the Account Controller and have a look and the methods.

您使用的是 ASP.NET MVC 4 的 Internet 模板吗?如果没有,我建议您使用 Internet 模板创建一个新的 ASP.NET MVC 项目,打开帐户控制器并查看方法。

As @user1 has rightly said one of the way you could do this is using

正如@user1 正确地说的那样,您可以执行此操作的方法之一是使用

 FormsAuthentication.SetAuthCookie(username, true);
 return Redirect(returnurl);

But as you say, you are using ASP.NET MVC 4. You should use WebSecurityClass.

但正如您所说,您使用的是 ASP.NET MVC 4。您应该使用WebSecurity类。

For example :

例如 :

WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");

Most of the work is done using the following methods and properties of the WebSecurity helper:

大多数工作是使用 WebSecurity 助手的以下方法和属性完成的:

Further Reading :

进一步阅读:

回答by Kalina

I suspect you are not setting the forms authentication cookie which is why the the redirect is not working. Ideally, in your login method, you will verify the username and password using some logic and when you are satisfied that the user is legit, you have to set the forms authentication cookie and then redirect the user

我怀疑您没有设置表单身份验证 cookie,这就是重定向不起作用的原因。理想情况下,在您的登录方法中,您将使用一些逻辑验证用户名和密码,当您对用户合法感到满意时,您必须设置表单身份验证 cookie,然后重定向用户

FormsAuthentication.SetAuthCookie(username, true);
return Redirect(returnurl);

If you don't set the authentication cookie, the redirect will not work since the request will still be treated as an unauthenticated request and the user will be sent back to the login page. You can find more information on forms login here

如果您不设置身份验证 cookie,重定向将不起作用,因为该请求仍将被视为未经身份验证的请求,并且用户将被发送回登录页面。您可以在此处找到有关表单登录的更多信息

http://msdn.microsoft.com/en-us/library/xdt4thhy%28v=vs.71%29.aspx

http://msdn.microsoft.com/en-us/library/xdt4thhy%28v=vs.71%29.aspx