Asp.net mvc ASP.NET MVC身份验证是否与数据库的替代源一起工作?
我有一个可以查询数据库并返回用户对象的服务 我希望使用上述服务在我的应用程序中实现MVC身份验证Asp.net mvc ASP.NET MVC身份验证是否与数据库的替代源一起工作?,asp.net-mvc,Asp.net Mvc,我有一个可以查询数据库并返回用户对象的服务 我希望使用上述服务在我的应用程序中实现MVC身份验证 ASP.NET MVC身份验证与实体框架无关吗?如果是,我应该覆盖哪个类实现来使用服务进行身份验证?最简单的方法是使用表单身份验证,我已经完成了,但还没有在生产中完成 LoginView模型 public class LoginViewModel { [Required] public string Username { get; set; } [R
ASP.NET MVC身份验证与实体框架无关吗?如果是,我应该覆盖哪个类实现来使用服务进行身份验证?最简单的方法是使用表单身份验证,我已经完成了,但还没有在生产中完成 LoginView模型
public class LoginViewModel
{
[Required]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
}
控制器动作
[HttpPost]
public ActionResult Login(LoginViewModel login)
{
if (!ModelState.IsValid)
{
ViewBag.Error = "Form is not valid; please review and try again.";
return View("Login");
}
if ((login.Username == "some@some.com" && login.Password == "NotVeryWise")) {
FormsAuthentication.SetAuthCookie(login.Username, false);
return RedirectToAction("Index", "Home");
}
ViewBag.Error = "Credentials invalid. Please try again.";
return View("Login");
}
Web.config
<system.web>
//...
//...
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" defaultUrl="~/Home" />
</authentication>
</system.web>
//...
//...
您现在可以在控制器上使用[Authorize]。需要了解的相关组件关系如下:
SignInManager
与UserManager
对话与UserStore
要重写的类是UserStore
。如果从Visual Studio模板创建ASP.NET应用程序,它将提供Microsoft.AspNet.Identity.EntityFramework.UserStore中的用户存储
您可以用自己的MyWcfServiceUserStore
(或您选择的任何名称)替换此用户存储
例如,如果将用户表示为MyUserType
,并且它使用Guid
作为唯一标识符:
public class MyWcfServiceUserStore
: IUserLockoutStore<MyUserType, Guid>,
IUserPasswordStore<MyUserType, Guid>,
IUSerTwoFactorStore<MyUserType, Guid>,
IUserEmailStore<MyUserType, Guid>
{
// Lots of methods to implement now
}
虽然这并不简单,但您自己实现所有必需的
UserStore
方法应该很简单。需要记住的一件事是,在一次web调用中,UserManager
可能会以相同的查询多次命中UserStore
,因此您必须充分利用缓存。如果不是数据库,您计划将用户名和密码信息存储在哪里?我能看到的信息有限,我会说不。你需要一个数据库来存储用户信息。是的,没有数据库,Identity就可以工作。但是,它将仅限于部署会话。每当你重新部署以前的数据时,都会丢失。谷歌“自定义asp.net标识”,你应该会找到一些有用的文章。@Claies-我正在数据库中存储信息。但要求不是从应用程序连接到数据库。应用程序必须通过wcf服务,该服务可以访问数据库以获取数据。
public class ApplicationUserManager : UserManager<MyUserType, Guid>
{
public ApplicationUserManager(IUserStore<MyUserType, Guid> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new MyWcfServiceUserStore(...));
//
// Further initialization here
//
return manager;
}
}