如何登录使用ASP.NET MVC4? [英] How to login using ASP.NET MVC4?
问题描述
下面是从我尝试code的某些位。这是行不通的; 登录后,用户不会被重定向到家庭/索引页,但在登录页面重新加载刚
Web.config文件:
<身份验证模式=表格>
<形式loginUrl =〜/帐号/登录/>
< /认证>...< defaultDocument>
<&文件GT;
<增加价值=〜/帐号/登录/>
< /文件>
< / defaultDocument>
控制器/ AccountController.cs:
公共类的AccountController:控制器
{
//
// GET:/帐号/登录
公众的ActionResult登录()
{
返回查看();
}
}
查看/帐号/的Login.aspx:
< ASP:登录ID =登陆=服务器DestinationPageUrl =〜/查看/主页/的Index.aspx>
...
< / ASP:登录>
您使用ASP.NET MVC 4的网络模板?如果不是,我建议你创建网络模板创建一个新的ASP.NET MVC项目,打开账户控制器,有一个外观和方法。
作为@ USER1正确地说,你可以做到这一点是使用的方法之一
FormsAuthentication.SetAuthCookie(用户名,真实);
返回重定向(RETURNURL);
但正如你说,你正在使用ASP.NET MVC 4,您应该使用<一个href=\"http://www.asp.net/web-pages/tutorials/security/16-adding-security-and-membership\">WebSecurity类。
例如:
WebSecurity.Login(model.UserName,model.Password);
返回RedirectToAction(指数,家);
大部分的工作是使用WebSecurity帮手以下方法和属性来完成:
-
<一个href=\"http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity.userexists%28v=vs.99%29.aspx\">WebSecurty.UserExists, <一href=\"http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity.createuserandaccount%28v=vs.99%29.aspx\">WebSecurity.CreateUserAndAccount.这些方法让你判断一个人是否已经注册和注册。
-
<一个href=\"http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity.isauthenticated%28v=vs.99%29.aspx\">WebSecurty.IsAuthenticated.此属性允许您确定当前用户是否登录,这是有用的,如果他们没有在已经登录的用户重定向到登录页面。
-
<一个href=\"http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity.login%28v=vs.99%29.aspx\">WebSecurity.Login, <一href=\"http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity.logout%28v=vs.99%29.aspx\">WebSecurity.Logout.这些方法或退出登录用户。
-
<一个href=\"http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity.currentusername%28v=vs.99%29.aspx\">WebSecurity.CurrentUserName.此属性是有用的显示当前用户的登录名(如果用户登录)。
-
<一个href=\"http://msdn.microsoft.com/en-us/library/gg569286%28v=vs.99%29.aspx\">WebSecurity.ConfirmAccount.如果设置了注册确认电子邮件此方法非常有用。 (详情载于博客文章使用了ASP.NET网页安全确认的功能描述。)
延伸阅读:
- 添加安全和成员到ASP.NET网页(剃刀)网站
- WebSecurity VS在ASP.NET MVC4 FormsAuthentication
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:
public class AccountController : Controller
{
//
// GET: /Account/Login
public ActionResult Login()
{
return View();
}
}
Views/Account/Login.aspx:
<asp:Login ID="login" runat="server" DestinationPageUrl="~/Views/Home/Index.aspx">
...
</asp:Login>
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.
As @user1 has rightly said one of the way you could do this is using
FormsAuthentication.SetAuthCookie(username, true);
return Redirect(returnurl);
But as you say, you are using ASP.NET MVC 4. You should use WebSecurity Class.
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:
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.)
Further Reading :
- Adding Security and Membership to an ASP.NET Web Pages (Razor) Site
- WebSecurity vs FormsAuthentication in ASP.NET MVC4
这篇关于如何登录使用ASP.NET MVC4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!