简体   繁体   English

如何使用asp.net-mvc创建基本用户登录?

[英]How to create a basic user login using asp.net-mvc?

I'm going to be building this from scratch with C# and asp.net on Entity/MVC frameworks using LINQ. 我将使用LINQ从头开始使用C#和asp.net构建实体/ MVC框架。

We've opted to not use the built in membership authorization. 我们选择不使用内置的会员资格授权。

What I am trying to figure out is if I have all my bases covered or if I am forgetting something. 我想弄清楚的是,如果我的所有基地都被覆盖,或者我忘记了什么。 Or if I am over complicating it. 或者如果我过度复杂化了。

Here is how I envision it. 这就是我设想它的方式。

I create a table that will house the user info, username, password. 我创建了一个表,其中包含用户信息,用户名和密码。 Move that to a model in my code. 将其移动到我的代码中的模型。

I create an Authentication service in my code. 我在代码中创建了一个身份验证服务。 Then when they log in I add the time to the table they logged in, and then on each page visit I check that time and if 30 minutes have elapsed I log them out and put them back on the login page otherwise I bring them to the page they requested. 然后,当他们登录时,我将时间添加到他们登录的表中,然后在每个页面上访问我检查时间,如果30分钟已经过去,我将它们记录下来并将它们放回登录页面,否则我将它们带到他们要求的页面。

Is this an ok way to go about it? 这是一个可行的方法吗? Do I really need to add an authentication check to each page controller? 我真的需要为每个页面控制器添加身份验证检查吗?

Which would Basically be. 哪个基本上是。 Services.Authentication.VerifyLogin()

If 30 minutes have passed log them out update table LoggedIn to false. 如果已经过了30分钟,则将更新表LoggedIn记录为false。 If within 30 minutes. 如果在30分钟内。 Update the LoggedInTime to current time. 将LoggedInTime更新为当前时间。

I think what you want is to use an MVC authentication cookie, instead of checking your database for the time they logged in: 我想你想要的是使用MVC身份验证cookie,而不是检查你的数据库他们登录的时间:

// sign in
FormsAuthentication.SetAuthCookie(username, false);
// sign out
FormsAuthentication.SignOut();

Ref. 参考。 Custom Authentication and ASP.NET MVC 自定义身份验证和ASP.NET MVC

Ref. 参考。 http://www.codeproject.com/Articles/578374/AplusBeginner-27splusTutorialplusonplusCustomplusF http://www.codeproject.com/Articles/578374/AplusBeginner-27splusTutorialplusonplusCustomplusF

Also, this is a good article on custom auth in MVC 4: 另外,这是关于MVC 4中自定义身份验证的好文章:

http://www.codeproject.com/Articles/601687/ASP-NET-MVC-4-Forms-Authentication-Customized http://www.codeproject.com/Articles/601687/ASP-NET-MVC-4-Forms-Authentication-Customized

If you're using MVC, you won't have to " add an authentication check to each page controller ", all you have to do is adding the [Authorize] attribute above the Actions that need authentication in your controllers and that's about it. 如果您正在使用MVC,则不必“ 向每个页面控制器添加身份验证检查 ”,您只需在控制器中需要身份验证的操作上方添加[Authorize]属性即可。

Here's an Authorization sample that might help you out: 这是一个可能帮助您的授权示例:
http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-7 http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM