Thursday, May 15, 2008

Logging in with MVC.net

The test site has been coming along pretty well, though not a lot of new stuff to speak of recently. I learned some new LINQ stuff, but nothing revolutionary.

At last, its time to add login capabilities. I installed the aspnet membership tables into the database, and setup the web.config. Then I created a new controller called LOGIN, and changed the LOGIN button (previously a dummy) to use it.




ShowLoginPage - I originally had a condition on the page that indicated which action to use depending on the current user's authenticated state. But, the point of this is to not do that kind of logic on the page, so I moved it to the ShowLoginPage action. If the user is authenticated, it redirects to the Logout action. Otherwise, it shows the login page.

Logout - Easy enough... Logout, then redirect to the home page. This shows that the controllers are aware of each other. I'm not sure if that's correct, but I don't know of another way to do it, so there you go.

Login - Users will login by email address. They'll have a username too, but that'll be for display purposes. The membership provider supports login by username, so I have to find the username by searching on the email address. The Find returns a collection of users. You can retrieve from the collection by username, but we don't have a username, so we have to enumerate to the first one. That was inconvenient. users[0] would've been swell.

I haven't handled a failed login yet. Essentially, I want it to load the same view but pass it an error message. That'll be new.

No comments: