如何在没有数据库的情况下使用 ASP.NET Identity [英] How to use ASP.NET Identity without a database

查看:16
本文介绍了如何在没有数据库的情况下使用 ASP.NET Identity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 MVC 5 项目中使用新的 ASP.NET Identity 实现自定义身份验证.

I am trying to implement custom authentication using the new ASP.NET Identity in an MVC 5 project.

我有一个单一用户名和密码,我想用它来限制用户可以通过控制器和视图上的 [Authorize] 标签查看网站的哪些页面.(简单)

I have a single username and password that I want to use to restrict which pages of the website the user can see via [Authorize] tags on controllers and views. (Easy)

我正在从 FormsAuthentication 模型迁移,这就像将凭据放在 web.config 中一样简单.

I am migrating from a FormsAuthentication model whereby this was as simple as putting the credentials in the web.config.

因为我只有一个用户名和密码我不想使用数据库作为 UserStore,而是 我希望 ASP.NET Identity 从一个数据库中检索用户名和密码web.config 中的自定义配置部分(不用担心那部分).

Because I only have a single username and password I don't want to use a database as the UserStore, instead I want ASP.NET Identity to retrieve the username and password from a custom configurationsection in the web.config (don't worry about that part).

经过大量搜索,我找不到不依赖数据库进行 ASP.NET Identity 身份验证的代码示例.

After much search, I can't find a code sample that doesn't rely on a database for ASP.NET Identity authentication.

所以我正在寻找一个代码示例,在身份验证时,用户可以输入自定义代码来检查用户名 &根据 web.config 的自定义 ConfigurationSection 中的凭据输入密码.

So i'm looking for a code sample that at the point of authentication, the user can put in custom code to check the username & password against the credentials in the custom ConfigurationSection of the web.config.

有人能指出我正确的方向吗谢谢.

Can someone please point me in the right direction thanks.

更新:我已经尝试查看此代码示例,但它甚至无法开箱即用地编译……很差.http://code.msdn.microsoft.com/Simple-Aspnet-Identiy-Core-7475a961

Update : I've tried looking at this code sample but it doesn't even compile out of the box.. poor. http://code.msdn.microsoft.com/Simple-Aspnet-Identiy-Core-7475a961

更新:我不想使用 FormsAuthentication 的原因是我正在编写一个将安装到 Web 应用程序中的 NuGet 包.NuGet 包要做的一件事是在 web.config 中创建一个自定义 ConfigurationSection,其中包括(除其他外)一个用户名和密码.我认为这会更安全,因为它不会改变当前在目标 Web 应用程序中的任何现有 FormsAuthentication 设置.

Update : The reason that I don't want to use FormsAuthentication is that I am writing a NuGet package that will be installed into a web application. One of the things the NuGet package will do is create a custom ConfigurationSection in the web.config that includes (among other things) a single username and password. I thought this would be safer as it wouldn't alter any existing FormsAuthentication settings currently in the target web application.

更新:我想我已经开始工作了.将很快发布调查结果.

Update : I think I have got it working. Will post findings soon.

--李

推荐答案

您不必迁移到 Identity 框架,FormsAuthentication 仍然有效.Andrew 是对的,在这里使用 Identity 框架毫无意义,因为它完全是为了管理用户.

You don't have to migrate to Identity framework, FormsAuthentication still works. And Andrew is correct, using Identity framework makes little sense here, since it is all about managing users.

但是,如果你坚持使用它,你可以实现自己的UserManagerIUserStore.在 Scott K.艾伦博文.请参阅底部的链接 - 实现示例 - 您可以使用其中的一些并转换为您的需求.

However, if you insist on using it, you can implement your own UserManager and IUserStore. Some guidance can be found in Scott K. Allen blog post. See the links on the bottom - samples of implementations - you can take some of these and convert to your needs.

我想你的 IUserStore 会很简单,因为只有一个用户,而且大多数方法不必实现.对于所需的那些(我认为你需要 FindUserById 和相关的),你需要通过 ConfigurationManager

I would imagine your IUserStore will be simple, because there is only one user and most of the methods don't have to be implemented. And for the ones required (I think you'll need FindUserById and related) you'll need to reach to web.config via ConfigurationManager

这篇关于如何在没有数据库的情况下使用 ASP.NET Identity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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