基本上我有实体:
public abstract class SocialNetworkAccount
{
public virtual string AccessToken { get; set; }
public virtual string ProviderUserId { get; set; }
public virtual string UserName { get; set; }
}
和子类:
public class FacebookAccount : SocialNetworkAccount
{
public virtual string Name { get; set; }
public virtual string Link { get; set; }
public virtual GenderEnum Gender { get; set; }
}
public class LinkedInAccount : SocialNetworkAccount
{
public virtual string LastName { get; set; }
public virtual string FirstName { get; set; }
}
我需要将DotNetOpenAuth.AspNet.AuthenticationResult映射到我的实体。
AuthenticationResult
有字符串属性Provider
AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication("...");
SocialNetworkAccount account = AutoMapper.Mapper.Map<SocialNetworkAccount>(result);
但我需要FacebookAccount
或LinkedInAccount
依赖于result.Provider
的实例。
任何帮助
答案 0 :(得分:1)
您应手动处理提供者值。我建议您创建AuthenticationResult到您需要的每种社交网络帐户类型的映射。然后在扩展方法中调用适当的映射:
public static SocialNetworkAccount ToSocialNetworkAccount(
this AuthenticationResult result)
{
switch(result.Provider)
{
case "facebook": return Mapper.Map<FacebookAccount>(result);
case "linkedin": return Mapper.Map<LinkedInAccount>(result);
// ...
default:
throw new ArgumentException("Unknown provider: " + result.Provider);
}
}
用法:
AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication("...");
SocialNetworkAccount account = result.ToSocialNetworkAccount();