Facebook authentication and Asp.Net Membership - c#

I'm trying to integrate facebook authentication with an asp.net site.
So if a user decides to register at the site they can do so by using their facebook credentials.
I'm currently at the point where I have the facebook access token and the user details and not sure how I should go from here.
The site uses asp.net membership authorization.
This is what I believe should happen in case a new user decides to register: (But not sure if this the the way to go)
0) User visits the site and decides to register using their facebook credentials.
1) The user providers their credentials and I receive an access token and their user information.
2) I store this information in my database and create an asp.net membership user with the data I received. (At this point I'd have to generate a password).
3) Log the user into the site so he can navigate freely.
I would appreciate some advice if I'm on the correct path and how I should go about generating the password. (I'm thinking in maybe combining the email and facebook userId, retrieve a hash and store.)
Thanks
UPDATE 1
I found this SO question where they suggest to use:
http://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.setauthcookie(v=VS.90).aspx

I think you approach is sound; what you effectively do is to replace the username/password authentication with the received facebook id, and let that id pass as a valid identifier in you application.
You say that you will need to generate a password in you application which I am note entirely sure about. It is true that you will need to create your user with a password as far as the membership provider in ASP.NET is concerned, but you could choose to fill in a random string if you only want the users to login using facebook connections.
Deciding which facebook attribute to bind to is also worth a bit of concideration. The natural choice is of course the facebook identifier since that is unqiue to the user, but if you choose to allow other authentication mechanisms later on - google open id for one - you might also benefit from storing the email from facebook etc.
Probably it will also be a good idea to auto generate a user name in you application that is not defined by facebook. If you choose the facebook identifier as login name you have a hard dependency on facebook making the introduction of new identity providers hard. If you choose a random identifier and an associative table establishing the connection between the facebook id and your id, you will also gain some flexibility later on. Choosing the somewhat more limiting email address might be a better choice if you want to have meaningful output from ASP.NET Login-controls like LoginStatus etc.

I haven't read the response below/above so this may have been covered but be warned that I ran into a serious problem with cookies not being set from within an iframe in IE. It was a bloody nightmare. I'm not sure if this has been fixed, if its fixable, but just be conscious of my experience and test thoroughly in all browsers.
Also checkout the .net open auth project. I haven't used it personally but it supposedly supports OAuth as well as OpenId & ICards, which could be helpful later on for additional integration points.

Related

Create my own authorization and authentication system in ASP.NET MVC

I'm working on an ASP.NET project, and i want to create and use my own authorization and authentication system to manage users (in a sql server database) like login, check authorization, and check authentication, i know in ASP.NET we have Identity but I want to create my own system.
so my question is do anyone have a good algorithm or a structure or just and idea for a custom system ?
Massive thanks in advance.
You can start from simple things, creating a model of User with ordinary fields Login, Password, and AuthController that will have two methods RegisterUser and SignIn.
RegisterUser - here you have to check if Login already exists, if not - insert data to table Users. Important thing - you have to choose algorithm to encrypt user password and save it in DB.
SignIn - method that will also check if user by Login and Password exists in DB (encrypt password from request by your algorithm and check if exists the same in Users table), if he is - you have to create a token and return it in response (to give an access to your portal).
Actually we can attach to it validation to a models (see https://fluentvalidation.net/), restore password logic with email notification and so on. A lot of examples with custom features of authorization and authentication you can find in the internet. Just separate your functionality into parts and google it.
You could fork from Identity Server 4 and create your own implementation. They have a good base to build a solution from and has integration with many app types using OpenID and OAuth. https://identityserver4.readthedocs.io/en/latest/

What's the recommended way to handle users who are registered by someone else (i.e. can't choose a password)?

I have an app where users are not self-registered, i.e. another user adds them to the system, and they are emailed with their login details. What's the best way to handle this in terms of creating the user and handling the password?
It's the sort of thing where a user is invited in to the system by someone else, answers a questionnaire, then doesn't need to log in again until some point in the future where they might be invited again by someone different, so ideally they will need to be reminded of their login details in the email rather than having to reset their password every time. Obviously in an ideal world people would just remember their passwords but the situation it's used in and the infrequency of access means that people simply don't remember, and the process needs to be as hassle-free as possible to ensure user participation.
The old version of the site used Webforms and ASP.Net Membership with encrypted passwords, which could be retrieved and sent to the user each time they are invited. What is the best practise for Identity, where the user does not supply their own password? Are there other options for logging in users such as:
A unique-link provided in the email which authenticates the user?
A separately encrypted 'passcode' field stored in the AspNetUsers
table which can be decrypted and sent each email?
It's worth pointing out that users with admin-level access to the site would still have the usual secure Identity user/pass setup, my problem is just with the lower level users.
I would forget completely about passwords in this case. ASP.NET Identity provides you with one-time codes that you can use for this.
This tutorial shows you in the section "Set up email confirmation" how to send one-time codes through mail (these are generated by ASP.NET Identity itself):
string html = "Please confirm your account by clicking this link: link<br/>";
You can easily modify that as well as the behavior when the user clicks that link to match what you want to do here.

Why to use ASP.NET Identity or Form Authentication?

I am creating a project not very large scale and came to create a system of registration and login. In which cases/projects you might want to use ASP.NET Identity and when you just want to create your own login system, whose creation is much easier?
As a result of Your practice?
Maybe I have to use Form Authenticator?
Do you want social login (google / facebook / twitter / linkedin / etc)?
Do you want two-factor authentication?
Do you require users to confirm an email address as part of registration?
YOU DO WANT TO 1-WAY ENCRYPT THE PASSWORD ...but how?
ASP.NET Identity2 makes doing a lot of these things easier, because they already wrote the code for a lot of this stuff -- password hashing, creating confirmation tokens (for registration / password reset / linking emails to an account), social logins, etc.
Also, #Tim Schmelter makes a very good point in his comment. Security is not easy to get right. When you roll your own registration and login, you are not only making more work for yourself, but you are probably going to leave holes in the registration and login system.
ASP.NET Identity is not an "all or nothing" tool. You can use pieces of it as needed. There is no reason why you can't borrow the password hashing and token generation/validation portions of it, and then use the (non-claims-based) FormsAuthentication to write the cookie. However if you want claims-based authentication and social logins, I would recommend staying away from FormsAuthentication.

Authentication using Azure

I have an Azure account and currently a Mobile Service setup with a SQL Database so that my Windows Store app can communicate with the database.
I have developed sites using ASP.NET WebPages authentication. And I need something similar for my Windows Store app.
I have successfully gone through the documentation and tutorials on the Windows Azure website and implemented ACS (Windows Live ID, Google, Yahoo!, and Facebook) - but the thing is - I don't want Google, Yahoo!, Facebook or even Windows Live ID or Microsoft Account) logins - I want my OWN login but it seems that they don't give you this option (correct me if I am wrong).
I need to allow users to signup from within my application (that means, providing their name, DOB, email, phone, address, etc) and shove it all in my database.
Now, after implementing Microsoft Account login with my Azure service, I found out that you can't even get the most basic information about any user who has logged in to your application - not even an email address.
I have spent hours searching online for something that could possibly help but I am running out of keywords - and have not hit a single related result yet.
Does anyone know if this is possible? How would we go about integrating login and signup with a Windows Store app that set/gets this data into/from a Windows Azure service?
Any code, samples, links, tutorials, documentation, etc would be highly appreciated.
You have gone down the road of hooking up external identity authentication, which in my opinion for an external facing web application is a better approach. Benefits are:
Your application is only responsible for Authorization not Authentication. There is a whole lot of work involved in Authentication and a large number of best practices. Best let those who know best take the burden of this. This doesn't mean you shouldn't try and understand it though.
If your site gets hacked you don't have to tell them that their username / email and password combo has been compromised and they will probably have to change there passwords on other sits.
You are also making sure that your users don't have to remember / manage yet another username / email address password combo
If you really want to do the Authentication then that is fine but you will need to do it yourself. Have a look at examples on Asp.Net Membership. This is not the only way and nor is it the best way but there are lots of examples.
Now if you decide you want to use external authentication I can give you some pointers to help with your current implementation.
First thing to note that the Id you get back from Live, Google, Facebook can only be assumed to be unique for that provider. Therefore if you want to keep a profile in your system for that identity and you want to use more than one provider you will need to implement it in such a way that you can keep the id unique in your system and help you associate it with a provider.
Website Authentication with Social Identity Providers and ACS Part 2 – Integrating ACS with the Universal Profile Provider
As you have found out not all of the Authentication providers return the same "claims". A claim is something that user claims to have, such as an email address, name, date of birth, etc. All the ones you can use by default via the ACS return Uid and some return a name and email address. What you have to do is fill in the gaps. When someone registers you will need to pull the relevant claims and then ask them to fill in the missing ones. You may also want to map the different claims in the ACS to a common name that you can use in your app as one provider might use slightly different names.
Federated Identity with Windows Azure Access Control Service
Just because you do not handle Authentication you still need to be responsible for keeping your application secure. Half of the work has been done for you so your code should be a lot lighter but you will still need to make use of roles.
Windows Azure Role Based authentication (ACS)
The really nice thing about this approach is you can implement your application the same why SO have done with there identity model. You can allow users to associate multiple identities against their profile meaning they can login how they want to.
If you choose not to use the built in providers for ACS you will need to implement your own Identity Provider using SAML, OpenId, etc...
You can look into the Windows Identity Foundation (WIF) for implementing WS-Trust or WS-Federation.
There is also ADFS which has the same set of support but uses Active Directory with WIF and Azure has its own version of AD that can be used.
There is also thinktecture identityserver which can jumpstart your venture into IdP land, but I have not used it myself yet.
If you want to go the OpenId route there is DotNetOpenAuth.
If you're looking to add custom identity to your Mobile Services app, check out Josh's post on custom auth: http://www.thejoyofcode.com/Exploring_custom_identity_in_Mobile_Services_Day_12_.aspx

Lightweight, registrationless, login system in webform

I would like to implement a lightweight registration less system for my asp.net page and need some direction to go
Basically I am currently at the point where I need session elements based on the user's username. But, since authentification is done using an other system, I would simply like to create session per username based on the success or failure of that other authentification system.
So, if authentification is succesfull using the information entered in the login page, simply create a new user in the system or something using the username provided (without, if possible, saving the password used).
I got a vanilla login page at the moment and would like some direction as to how to proceed while using as much of the vanilla infrastructure as possible.
Thanks you all :)
What your talking about is claims-based authentication. You trust a third-party service to have authenticated the user and trust that the information that the service tells you about that user is correct.
See what options you have for the service to pass you those details. You can use full-on SAML-based claims and Windows Identity Foundation may take on pretty much all of the work for you. If the third-party application is acting as a proxy, you could have it inject a HTTP header with the user name in. Or you could have a form, but someone or something is going to have to enter the data and post that form (you can do it automatically from Javascript when the site is launched).
If you want to use a form, it could be a modified Forms-based logon screen that doesn't check the password, just creates the Forms Authentication cookie.
If you want to use a more 'bespoke' scheme, you could create a custom security principal implementing IPrincipal, you can transparently inject this into your application and have it behave just like it would if you were doing 'proper' authentication.
Either way, don't try to mess with the way that ASP.NET security operates, just concentrate on the best way to get the information to ASP.NET via the security principal.

Categories

Resources