如何使用EF,linq和Web表单创建登录页面 [英] How do I make login page using EF , linq and web forms

查看:97
本文介绍了如何使用EF,linq和Web表单创建登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

H
我正在制作一个ASP.NET Web Forms应用程序,我正在寻找一种方法来做一个好的,安全的,易于制作的登录页面。

我的主要问题是我已经拥有一个SQL数据库,而且我正在使用EF + LINQ来操作它。因为我不能使用Idenity,Owin和Katana(如果现在有人可以在VS 2012中制作它也会对我有帮助)。

请帮我选择使用EF + LINQ + WEB表格制作登录页面或一些样本的技术。



我是什么尝试过:



投掷:Owin,Idenity和Sql Membership。首先是坏的,因为我需要迁移,第二,因为我需要使用Linq

H I am making an ASP.NET Web Forms app and i'm searching for way to do a good, secured and easy to make Login Page .
My main problem that i already have a SQL DataBase and i'm using EF + LINQ to operate with this . Because of that i can't use Idenity, Owin and Katana ( if some one now how it can be make in VS 2012 it will helps me too ) .
Please help me to choose technology for make Login Page or some samples using EF +LINQ +WEB Forms .

What I have tried:

Make it throw : Owin, Idenity and Sql Membership . First is bad because i need migration for this , second because i need to use Linq

推荐答案

使用EF和LINQ与使用Identity或SQL Membership无关。 EF是一个对象关系映射器(ORM),您将使用它作为旧的普通ADO.NET的替代品来处理数据。另一方面,LINQ用于查询对象和集合。例如,从EF生成的对象。 LINQ还提供了一些扩展方法,您可以使用它们在查询时轻松操作对象/集合。话虽如此,您仍然可以使用LINQ和EF来使用ASP.NET身份。



但如果您不想使用Indentity或Membership,那么您可以类似这样的事情:



Using EF and LINQ has nothing to do with using Identity or SQL Membership. EF is an object relational mapper (ORM) and you'll use it as a replacement for good-old-plain ADO.NET to work with data. LINQ on the other hand is used for querying objects and collections. For example objects generated from your EF. LINQ also provides a handfull of extension methods that you can use to easily manipulate the objects/collections when querying. Having that said, you could still use LINQ and EF to work with ASP.NET Identity.

But if you don't want to use Indentity or Membership then you could something like this:

private string GetUserPassword(string loginName) {  
            using (DBEntities db = new DBEntities()) {  
                var user = db.SYSUsers.Where(o => o.LoginName.ToLower().Equals(loginName));  
                if (user.Any())  
                    return user.FirstOrDefault().PasswordEncryptedText;  
                else  
                    return string.Empty;  
            }  
} 

protected void Button1_Click(object sender, EventArgs e){  
		string loginName = txtLoginName.Text.Trim();
        	string password = GetUserPassword(loginName);  
  
                if (string.IsNullOrEmpty(password))  
                    Response.Write("The user login or password provided is incorrect.");  
                else {  
                    if (txtPassword.Text.Equals(password)) {  
                        FormsAuthentication.SetAuthCookie(loginName, false);  
                        Response.Redirect("~/Home.aspx");
   
                        //you can also use the FormsAuthentication.RedirectFromLoginPage() 
                        //to redirect 
                    }  
                    else {  
                        Response.Write("The password provided is incorrect.");  
                    }  
                } 
} 





请注意,上面的代码只是一个简单的代码段,用于描述使用EF和LINQ对WebForms中的用户进行身份验证。您可能需要根据数据库和控件更改部分代码。



Note that the code above is just a simple snippet describing the use of EF and LINQ to authenticate users in WebForms. You may need to change some of the code based on your database and controls.


这篇关于如何使用EF,linq和Web表单创建登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆