📜 ⬆️ ⬇️

uLogin on the ASP.Net website

It took a few days ago to tie uLogin to the asp.net site, but on the move to find the finished code somehow did not work. Definitely someone will face this issue ...


On my choice whether to use uLogin (a review on Habré ) in my projects were affected by some numbers . We will not go into the credibility of the resource, for me, for some reason, that was enough. The service is quite simple and conveniently made - you specify the address of your site, select the buttons of the required authorization providers and get a couple of pieces of code that need to be placed on your site. If the html code doesn’t particularly depend on what the site is created on, then the proposed php code somehow didn’t fit me. The result was what turned out :)

I placed the resulting html on the master page of the site.
')
< script src ="http://ulogin.ru/js/ulogin.js" ></ script > < div id ="uLogin" x-ulogin-params ="display=small;optional=email,sex;fields=first_name,last_name,photo;providers=vkontakte,facebook,twitter,odnoklassniki,google;hidden=yandex,mailru,livejournal,openid,liveid;redirect_uri=http%3A%2F%2Fmysite.ru%2Fauth.aspx" ></ div > * This source code was highlighted with Source Code Highlighter .
  1. < script src ="http://ulogin.ru/js/ulogin.js" ></ script > < div id ="uLogin" x-ulogin-params ="display=small;optional=email,sex;fields=first_name,last_name,photo;providers=vkontakte,facebook,twitter,odnoklassniki,google;hidden=yandex,mailru,livejournal,openid,liveid;redirect_uri=http%3A%2F%2Fmysite.ru%2Fauth.aspx" ></ div > * This source code was highlighted with Source Code Highlighter .
  2. < script src ="http://ulogin.ru/js/ulogin.js" ></ script > < div id ="uLogin" x-ulogin-params ="display=small;optional=email,sex;fields=first_name,last_name,photo;providers=vkontakte,facebook,twitter,odnoklassniki,google;hidden=yandex,mailru,livejournal,openid,liveid;redirect_uri=http%3A%2F%2Fmysite.ru%2Fauth.aspx" ></ div > * This source code was highlighted with Source Code Highlighter .
< script src ="http://ulogin.ru/js/ulogin.js" ></ script > < div id ="uLogin" x-ulogin-params ="display=small;optional=email,sex;fields=first_name,last_name,photo;providers=vkontakte,facebook,twitter,odnoklassniki,google;hidden=yandex,mailru,livejournal,openid,liveid;redirect_uri=http%3A%2F%2Fmysite.ru%2Fauth.aspx" ></ div > * This source code was highlighted with Source Code Highlighter .


The address redirect_uri indicates where the user will be sent (with the uLogin token) after he passes authorization through the provider he chooses. Having received token, you need to go to uLogin and ask for information about the user.

  1. string link = string .Format ( "http://ulogin.ru/token.php?token={0}&host={1}" , Request.Form [ "token" ],
  2. Request.ServerVariables [ "SERVER_NAME" ]);
  3. WebRequest reqGET = WebRequest.Create (link);
  4. string answer = "" ;
  5. using (WebResponse resp = reqGET.GetResponse ())
  6. {
  7. using ( Stream stream = resp. GetResponseStream ())
  8. {
  9. if (stream! = null )
  10. using (StreamReader sr = new StreamReader (stream))
  11. {
  12. answer = sr.ReadToEnd ();
  13. }
  14. }
  15. }


In response, we get a jSON string, which I decided to part with as follows:

  1. public class JSONHelper
  2. {
  3. public static T Deserialise <T> ( string json)
  4. {
  5. T obj = Activator. CreateInstance <T> ();
  6. using (MemoryStream ms = new MemoryStream ( Encoding .Unicode.GetBytes (json)))
  7. {
  8. DataContractJsonSerializer serialiser = new DataContractJsonSerializer (obj.GetType ());
  9. obj = (T) serialiser.ReadObject (ms);
  10. }
  11. return obj;
  12. }
  13. }
  14. public class iUser
  15. {
  16. public string Network { get ; set ; }
  17. public string Uid { get ; set ; }
  18. public string FirstName { get ; set ; }
  19. public string LastName { get ; set ; }
  20. public string Email { get ; set ; }
  21. public string Sex { get ; set ; }
  22. public override string ToString ()
  23. {
  24. return string .Format ( "network = {0}, uid = {1}, first_name = {2}, last_name = {3}" , Network, Uid, FirstName, LastName);
  25. }
  26. }


You can ask several other attributes for uLogin. I added some to the optional parameter so as not to upset the user with unnecessary questions (let’s enter the email address if the provider cannot transfer it)

It remains to deserialize:

  1. iUser usr = JSONHelper.Deserialise <iUser> (answer);


Well, then everyone decides for himself what to do with the data obtained. As an example - put yourself in the database and send the user a cookie with a token for authorization on the site.

I tested on my accounts. Maybe more extensive use will be able to identify existing bugs.
I hope that the decision was not only short, but also useful.

Source: https://habr.com/ru/post/142631/


All Articles