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
How to login using ASP.NET MVC4?
提问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 助手的以下方法和属性完成的:
WebSecurty.UserExists, WebSecurity.CreateUserAndAccount. These methods let you determine whether someone is already registered and to register them.
WebSecurty.IsAuthenticated. This property lets you determine whether the current user is logged in. This is useful to redirect users to a login page if they have not already logged in.
WebSecurity.Login, WebSecurity.Logout. These methods log a user in or out.
WebSecurity.CurrentUserName. This property is useful for displaying the current user's logged-in name (if the user is logged in).
WebSecurity.ConfirmAccount. This method is useful if you set up email confirmation for registration. (Details are described in the blog post Using the confirmation feature for ASP.NET Web Pages security.)
WebSecurty.UserExists,WebSecurity.CreateUserAndAccount。这些方法让您可以确定某人是否已经注册并注册他们。
WebSecurty.IsAuthenticated。此属性可让您确定当前用户是否已登录。如果用户尚未登录,这对于将用户重定向到登录页面很有用。
WebSecurity.Login,WebSecurity.Logout。这些方法使用户登录或注销。
WebSecurity.CurrentUserName。此属性可用于显示当前用户的登录名(如果用户已登录)。
WebSecurity.ConfirmAccount。如果您为注册设置电子邮件确认,则此方法很有用。(详细信息在博客文章使用 ASP.NET 网页安全性的确认功能中进行了描述。)
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

