📜 ⬆️ ⬇️

.NET library for Loginza service

Many people know such an interesting service as Loginza . The service is still quite young, and many things that we 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:

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 .
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 .


The class constructor takes 2 parameters: the widget ID and the secret key.
The IsSecureCheck property indicates the need to use secure token checking. Also in the class there is a property ServiceUri , which can be changed if the address of the checking service changes.

The AuthInfo method returns a dynamic object (therefore, the class only works in .NET 4), whose fields are formed based on the Json data returned by the service.

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 may vary (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 authenticated .

I will be glad comments and improvements.

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


All Articles