Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 230
PussInBoots Avatar asked Nov 04 '13 13:11

PussInBoots


2 Answers

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.

like image 189
PussInBoots Avatar answered Nov 12 '22 15:11

PussInBoots


You can still do something similar, basically in Startup.Auth.cs you will need to add extra data to the AuthenticationDescription when you enable the auth provider:

        var desc = new AuthenticationDescription();
        desc.Caption = "Google";
        desc.AuthenticationType = "Google";
        desc.Properties["Img"] = "<img>";
        app.UseGoogleAuthentication(new GoogleAuthenticationOptions() { Description = desc });

And then use the @p.Properties["Img"] in your button like you were doing before inside of the _ExternalLoginListPartial view

        <legend>Use another service to log in.</legend>
        <p>
        @foreach (AuthenticationDescription p in loginProviders) {
            <button type="submit" class="btn" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" title="Log in using your @p.Caption account">@p.AuthenticationType</button>
        }
        </p>
like image 31
Hao Kung Avatar answered Nov 12 '22 16:11

Hao Kung