.NET library for Loginza service

    Many people know such an interesting service as Loginza . The service is still quite young, and many things that I would like to have are not yet implemented for it.

    Just yesterday, I needed a simple way to work with information about the success or failure of user authentication. I sketched a simple Helper to work with this service, you can download it here .


    How to work with the library?


    Here I gave an example of code that simply shows how to work with the library: The class constructor takes 2 parameters: widget ID and secret key. The IsSecureCheck property indicates the need to use secure token validation. There is also a ServiceUri property in the class , which can be changed if the address of the verification service changes. The AuthInfo method returns a dynamic object (therefore, the class only works in .NET 4), the fields of which are formed on the basis of Json data returned by the service.

    public class AccountController : Controller
    {

      [AcceptVerbs(HttpVerbs.Post)]
      public ActionResult SignIn(string token)
      {
        try
        {
          if (token == null) throw new ArgumentNullException("token");
          var loginza = new Loginza.Api.LoginzaHelper(Convert.ToInt32(ConfigurationManager.AppSettings[@"WidgetId"]), ConfigurationManager.AppSettings[@"SecureKey"]) { IsSecureCheck = true };
          var authInfo = loginza.AuthInfo(token);
          if (authInfo.error_type != null)
            throw new AuthException(authInfo.error_message ?? "Unknown error");
          else
          {

            FormsAuthentication.SetAuthCookie(authInfo.name.full_name, false);
          }

          return RedirectToAction("Index", "Home", null);
        }
        catch
        {
          return View("Error");
        }
      }
    }

    * This source code was highlighted with Source Code Highlighter.







    This implementation does not take into account a small problem - I noticed that for different providers the composition of the fields in the Json response can change (in the example of the field for the Google provider), which means that the set of fields in the dynamic object will differ depending on which provider the user is authenticated through .

    I will be glad to comments and improvements.


    Also popular now: