ASP.NET Identity外部认证提供程序自定义图标

时间:2013-11-04 13:50:54

标签: asp.net oauth-2.0 openid simplemembership asp.net-identity

使用SimpleMembership,您可以向外部身份验证提供程序按钮添加一个图标,如下所示:

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

然后在您的视图中显示如下:

@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标识(​​?):

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

如何使用ASP.NET Identity(控制器和视图)实现相同的功能?

2 个答案:

答案 0 :(得分:6)

另一种方法:

采取了这个博客中的一些内容(使用zocial图标,但我发现那些是过度的 - 请参阅css文件,你会知道我的意思): http://www.beabigrockstar.com/pretty-social-login-buttons-for-asp-net-mvc-5/

并且这样做了:

Startup.Auth.cs(没有额外的东西,只是MVC 5应用程序的标准默认内容)

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);
}

查看:

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

在上面的其他解决方案/答案中使用类而不是不那么优雅的样式属性。

答案 1 :(得分:3)

您仍然可以执行类似的操作,基本上在Startup.Auth.cs中,您需要在启用身份验证提供程序时向AuthenticationDescription添加额外数据:

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

然后在你的按钮中使用@ p.Properties [“Img”],就像你在_ExternalLoginListPartial视图中所做的那样

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