SimpleMembership 및 WebSecurity와 함께 내 데이터베이스를 어떻게 사용합니까? MVC4 보안이란 무엇입니까?
MSDN 기사 및 SO 게시물을 포함하여이 주제에 대한 모든 것을 읽었지만 여전히 매우 혼란스럽고 혼란 스럽습니다.
질문
다음 질문에 답해주십시오 (가능한 경우 간략하게).
무엇 SimpleMembership / SimpleMembershipProvider은 ( WebMatrix.WebData ) 그리고 / 그들에 대한 책임을 무엇입니까?
WebSecurity ( WebMatrix.WebData ) 란 무엇입니까 ?
Membership ( System.Web.Security ) 클래스 란 무엇입니까 ?
MVC4가 UserProfile 테이블과 webpages_Membership 테이블을 만드는 이유는 무엇 입니까? 그것들은 무엇이며 차이점은 무엇입니까? MVC4가 생성하는 UserProfile 클래스는 무엇입니까?
UsersContext 클래스 는 무엇입니까 ?
이 모든 것이 사용자 인증을 위해 어떻게 함께 작동합니까?
내 상황
이러한 질문은 다음 문제로 이어집니다.
사용자 (ID, 사용자 이름, 암호)가있는 기존 데이터베이스가 있다고 가정합니다. 새 MVC4 응용 프로그램을 만들고 양식 인증을 사용하고 있습니다. 사용자 암호는 암호화 된 형식 (bcrypt 아님)으로 데이터베이스에 저장됩니다.
MVC4에서 작동하도록하려면 어떻게해야합니까?
사용자 지정 MembershipProvider 를 만들어야 합니까?
지금까지의 나의 지식
내가 이해할 수있는 한 WebSecurity 는 MembershipProvider 와 상호 작용하는 정적 클래스 (Module)입니다 . MembershipProvider는 ValidateUser , CreateUser , ChangePassword 와 같은 특정 함수의 작동 방식을 설명하는 클래스입니다 .
내 문제를 해결하려면 사용자 지정 MembershipProvider를 만들고 WebSecurity에 새 MembershipProvider를 사용하도록 지시해야한다고 가정합니다.
하사품?
나는이 질문에 대해 현상금을 걸 었으며 뛰어난 답변을 위해 Andy Brown에게 수여 할 예정입니다.
빠른 답변은 각 인용문 아래의 요약을 참조하고 자세한 내용은 단락을 참조하십시오. 또한 권위있는 출처에 대해서는 끝에있는 참조 섹션을 참조하십시오.
요약
1. SimpleMembership / SimpleMembershipProvider (WebMatrix.WebData)는 무엇이며 무엇을 담당합니까?
SimpleMembership ( SimpleMembershipProvider
및을 모두 포함하는 용어 SimpleRoleProvider
)은 누구나 사용할 수있는 안전한 암호 저장소를 사용하여 80 % 플러그 앤 플레이 인증 및 권한 부여 프레임 워크를 구현하는 깨끗하고 빠른 방법을 제공합니다.
2. WebSecurity (WebMatrix.WebData) 란 무엇입니까?
WebSecurity
함께 작동하는 일반 회원의 작업을위한 도우미 클래스입니다 Membership
및 OAuthWebSecurity
. 역할은 여전히를 통해 별도로 액세스됩니다 Roles
.
3. Membership (System.Web.Security) 클래스 란 무엇입니까?
Membership
사용자 설정 및 작업을 관리하는 원래 ASP.NET 멤버 자격 구현의 정적 클래스입니다. 많은 사용자 작업은에서 반복하지 않고 여기에서 수행됩니다 WebSecurity
. 둘 다 귀하가 선택한 동일한 공급자를 사용합니다.
4. MVC4가 UserProfile 테이블과 webpages_Membership 테이블을 만드는 이유는 무엇입니까? 그것들은 무엇이며 차이점은 무엇입니까? MVC4가 생성하는 UserProfile 클래스는 무엇입니까?
두 테이블은 서로 다른 기능을 수행합니다. webpages_Membership
스키마는 프레임 워크에 의해 제어 및 자격 증명에 사용되는 UserProfile
스키마가 우리에 의해 제어 우리는 사용자에 대해 저장하려는 속성에 사용됩니다.
5. UsersContext 클래스 란 무엇입니까?
그것은이다 DbContext
(의 일부 DbContext의 API MVC의 인터넷 응용 프로그램 템플릿에 의해 시작으로 제공). 그것의 유일한 임무는 UserProfile
우리가 작업 할 수 있도록 클래스 를 포함하는 것입니다 InitializeSimpleMembershipAttribute
.
6.이 모든 것이 사용자 인증을 위해 어떻게 함께 작동합니까?
이것은 이제 위의 요약과 아래의 세부 사항에서 분명합니다. 사용 : WebSecurity
일반적인 작업을 위해; UserProfile
사용자에 대해 저장할 사용자 지정 속성의 경우 UsersContext
(Visual Studio "MVC 인터넷 응용 프로그램"템플릿에서) 액세스합니다. 방법이 있거나 없을 Membership
때 ; 그리고 역할. 사용 예제를 보려면 VS 템플릿의 컨트롤러를 사용하십시오.WebSecurity
OAuthWebSecurity
Roles
편집 . 누군가 여기까지 왔을 경우
기존 데이터베이스가 있다고 가정합니다.
기존 데이터베이스가 있고 사용자 지정 멤버 자격 공급자를 작성하는 유일한 이유가 레거시 암호 저장 방법을 처리하는 것이라면 해결 방법을 사용할 수 있습니다. 이것은 이전 암호 저장소에서 SimpleMembership 알고리즘 ( Rfc2898DeriveBytes
클래스 사용)으로 이동할 수있는 경우에만 작동합니다 . 자세한 내용은 각주를 참조하십시오.
멀리 이동할 수없는 경우, 예 당신은 당신이 할 수있는 특정 암호 알고리즘을 사용하여 자신 만 제공 만들어야 할 예정 로부터 파생시켜를SimpleMembershipProvider
.
참고 : SimpleMembershipProvider
합니다 암호가 그들을 암호화하지 해시 . 차이점과 이것이 중요한 이유를 모른다면 사용자 지정 보안으로 자체 공급자를 수행하기 전에 두 번 생각하십시오.
세부 묘사
1. SimpleMembership / SimpleMembershipProvider 란?
모든 것이 어떻게 조화를 이루는 지 이해하려면 역사를 이해하는 것이 도움이됩니다.
- 2005 년 ASP.NET은 ASP.NET Membership 시스템을 도입했습니다.
- 이 시스템은 공급자를 사용하여 계정 및 역할 등을 관리하는 데 사용되는 공통 인터페이스에서 구현 세부 정보를 추상화했습니다.
- 또한 기본 "사용자 프로필"기능을 제공했습니다 (따라서 사람들이 피하는 경향이있는 단일 열 xml 필드에 저장 됨).
- SimpleMembership은 ASP.NET 멤버십 시스템에 연결하는 공급자로서 2010 년에 세계에 출시되었지만 OAuth 인증 및 열당 속성 사용자 프로필 저장소 (원본에 사용 된 단일 열 저장소 대신)도 허용합니다. 이행).
SimpleMembershipProvider
ExtendedMembershipProvider
원래 공급자 구현 을 확장하기 위해 구현
It is Open Source on codeplex (mirrored on github). As far as security goes you can therefore assess the code yourself, clone it, change it etc. You should take your own view on the benefits and drawbacks of open source security, and cook that up up with a pinch of NIH. (Personal view: I use it sometimes, I don't use it other times)
ExtendedMembershipProvider
in itself adds commands like GeneratePasswordResetToken
to the old membership provider apis.
2.What is WebSecurity (WebMatrix.WebData)?
WebSecurity
간단한 접근을 제공 SimpleMembershipProvider
하고 일반적인 작업을 한곳에서 쉽고 접근 할 수 있도록 만드는 단순한 파사드 또는 도우미 클래스 입니다. 도움이 필요하며 원래 프레임 워크의 확장은 ExtendedMembershipProvider
기존 클래스 중 일부 Membership
만으로는 충분하지 않기 때문입니다. 예 :
WebSecurity.CurrentUserName
-현재 로그인 한 사용자의 이름을 얻습니다.WebSecurity.CreateUserAndAccount
. 동시에 사용자 생성 및 사용자 프로필 속성 설정 (예 :WebSecurity.CreateUserAndAccount(userName, pw, new { Email = model.Email });
WebSecurity.InitializeDatabaseConnection
-멤버십에 사용할 신규 / 기존 데이터베이스를 빠르게 설정하고, 사용자 ID 열과 사용자 자연 키 식별자 등을 선택합니다.ResetPassword
사용자 암호 재설정GeneratePasswordResetToken
및 더 많은
These methods generally defer to the provider you are using, they don't just depend on SimpleMembership, and they tie together objects like your provider and Membership
to provide a common point to do membership functions.
Note there is also OAuthWebSecurity
which is the equivalent of WebSecurity
for OAuth authentication.
3.What is the Membership (System.Web.Security) class?
Membership
is from the original implementation; it manages user settings and performs user-related operations using the basic MembershipProvider
implementation which ExtendedMembershipProvider
now extends. It is a static class, so is available anywhere you declare the namespace, and is therefore an easy way to, for example, retrieve the current user: Membership.GetUser
There is confusion caused by the fact that WebSecurity
does some things and not others, and Membership
does some things and not others. If you view WebSecurity
as a toolkit for higher level operations, and Membership
as a toolkit to do things to a user, you'll be ok; they work together on your provider.
4.Why does MVC4 create a UserProfile table and a webpages_Membership table? What are they for and what is the difference? What is the UserProfile class that MVC4 creates?
webpages_Membership
is a table with a fixed schema that we leave alone, and allows the provider to do the basic account operations, mainly storing credentials.UserProfile
is a table that we customise to store information against a user account, and have that made available in a strongly typed format through theUserProfile
class.- There is an extra table called
webpages_OAuthMembership
which does the same job aswebpages_Membership
, but for OAuth login providers that you want to integrate with.
The magic of this setup is that a single user can have a membership login on your own site, and any number of OAuth logins with different providers like google, facebook, and they all share a common profile stored in UserProfile
Generally if a table starts with webpages_
, it means there is an API to access it. The UserProfile
table is represented by the UserProfile
class in your UsersContext
(if you use the default MVC Internet Application template). Therefore we access this through the usual methods we would use with any class contained in a DbContext
.
UserProfile
is very code-first friendly: you can add columns (like the user's Email
address), and then set up a migration to include that column in your database on your next release (if you like using migrations). In fact, the UserProfile
table does not have to be called that - you can change that using the WebSecurity.InitializeDatabaseConnection
call, [Table("UserProfile")] public class UserProfile
, and your own migrations.
5.What is the UsersContext class?
This is from the MVC Internet Application template provided in Visual Studio New Project. The first thing I do is make sure that it shares a common connection string with my own database context (assuming the membership tables are in the same database). You can change this and decouple them later if you want.
You don't need to have it separate to your own context - that is only necessary if you want to store membership information in a different database now or in the future If you get rid of it you can just change references to UsersContext
to your own context, adjusting Database.SetInitializer
.
References:
Using SimpleMembership With ASP.NET WebPages - Matthew Osborn - This is the original reference about SimpleMembership and what it is, why it is, and what it does:
MSDN - Introduction to Membership - Membership is still at the core of SimpleMembership, so it helps to understand a bit about it.
- codeplex source (mirrored on github).
WebSecurity
OAuthWebSecurity
SimpleMembershipProvider
ExtendedMembershipProvider
SimpleRoleProvider
Membership
Roles
DbContext
and the DbContext API
EDIT Footnote: the detail for doing a rolling password upgrade
- Add a property to
UserProfile
which stores what password version the account is on (e.g. 1 for legacy, 2 for SimpleMembership) - In the "Login" Action, write code so that:
- If they are on your SimpleMembership password version, you do a normal login
- If they are on the legacy password version, you:
- check it using your old method
- if it is correct you reset it using
ResetPassword
thenChangePassword
to use the SimpleMembership version, this will update the field to the new password version - and finally update the Password version on the
UserProfile
- Update any other AccountsController methods that use password in a similar way.
- Live with the hacky workaround and coupling to the
webpages_Membership
table we aren't meant to touch as you didnt have to write a new custom provider.
It is possible to make all this transactional with TransactionScope
. The only nasty thing going on is the extra code in the controller, and the coupling to webpages_Membership
.
'development' 카테고리의 다른 글
부호있는 변수와 부호없는 변수의 차이점은 무엇입니까? (0) | 2020.11.29 |
---|---|
Rails 3. ActiveAdmin이 사용할 도우미를 추가하는 방법은 무엇입니까? (0) | 2020.11.29 |
jquery-ui 및 webpack, 모듈로 관리하는 방법은 무엇입니까? (0) | 2020.11.29 |
Python과 JavaScript를 통합하는 가장 좋은 방법은 무엇입니까? (0) | 2020.11.28 |
최대 합을 가진 부분 행렬을 얻습니까? (0) | 2020.11.28 |