I am really confused right now on how would I implement authentication in ASP.NET MVC 5. This is my first time using ASP.NET MVC and I am used to desktop apps. There are a lot of terms thrown around like OWASP, OAuth, FormsAuthenticationTicket, and others that I am really having a hard time following.
I've decided is just to make a simple log in page for starters. Just check database for username and password and if exists, then user is authenticated. So far, based on what I've read, I can do so implementing IAuthenticationFilter. I have implemented OnAuthentication like so:
public void OnAuthentication(AuthenticationContext context)
{
IIdentity ident = context.Principal.Identity;
if (!ident.IsAuthenticated || !ident.Name == "randy") // Name is just for testing
{
context.Result = new HttpUnauthorizedResult();
}
}
For OnAuthenticationChallenge, I just have code that redirects to a LogIn action method if user not authenticated (if result is HttpUnauthorizedResult and if user is not authenticated. Thinking about it, its like the same as logic OnAuthentication so why couldn't I just redirect then and there. I based these code on web examples).
In LogIn action method, there is a UserRepository that checks if a user exists. But if the user exists, I don't know how to set it the context's Principal. I don't know if I am doing this right so please comment on this.
I guess my questions would be:
Is what I am doing correct?
Why don't the examples in the web just redirect in OnAuthentication if the user is not authenticated. Why wait for OnAuthentication to execute?
If what I am doing is correct, how do I set the Principal from the LogIn Action method? How do I clear it? And if I understand correctly, is this synonymous to logging in/logging out (setting principal/clearing principal)?
If all I am doing is wrong, or even if it is right, could you direct me to where can I start learning authentication for ASP.NET MVC? When I search, the topics are for a specific implementation of something which mostly assumes understanding of some concepts. I'm having a hard time of where to start.