Search code examples
asp.netoauth-2.0openidsimplemembershipasp.net-identity

ASP.NET Identity external authentication provider custom icon


With SimpleMembership you can add an icon to the external authentication provider buttons like this:

SimpleMembership:

Dictionary<string, object> FacebooksocialData = new Dictionary<string, object>();
FacebooksocialData.Add("Icon", "/content/images/gui/loginFacebook.png");
OAuthWebSecurity.RegisterFacebookClient(
    appId: "x",
    appSecret: "x",
    displayName: "Facebook",
    extraData: FacebooksocialData);

And then display them like this in your view:

@foreach (AuthenticationClientData p in Model)
{
    <button class="externalLoginService" style="cursor:pointer;color:transparent;border:none;background:url(@p.ExtraData["Icon"]);width:94px;height:93px;margin-right:20px;" type="submit" name="provider" value="@p.AuthenticationClient.ProviderName" title="Log in with @p.DisplayName">@p.DisplayName</button>
}

ASP.NET Identity(?):

app.UseFacebookAuthentication(
   appId: "x",
   appSecret: "x");

How to achieve the same thing using ASP.NET Identity (controller and view)?


Solution

  • Another way of doing it:

    Took some of what is in this blog (uses zocial icons but I found those to be overkill - see css file and you'll know what I mean): http://www.beabigrockstar.com/pretty-social-login-buttons-for-asp-net-mvc-5/

    And did it like this:

    Startup.Auth.cs (no extra nothing, just the standard default stuff from an MVC 5 app)

    app.UseFacebookAuthentication(appId: "x", appSecret: "x");
    app.UseGoogleAuthentication();
    

    CSS:

    .socialLoginButton {
        cursor:pointer;color:transparent;border:none;width:94px;height:93px;margin-right:20px;
    }
    
    .socialLoginButton.facebook {
        background:url(/content/images/gui/loginFacebook.png);
    }
    .socialLoginButton.google {
        background:url(/content/images/gui/loginGoogle.png);
    }
    

    View:

    <button type="submit" class="externalLoginService socialLoginButton @p.AuthenticationType.ToLower()" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" title="Log in with @p.Caption">@p.AuthenticationType</button>
    

    Using classes instead of the not so elegant style attribute in the other solution/answer above.