【问题标题】:Is it possible to use Amazon.com as an authentication provider是否可以使用 Amazon.com 作为身份验证提供商
【发布时间】:2011-02-03 08:45:44
【问题描述】:

我以为我可以找到有关此主题的信息,但今天我的 google-fu 似乎很弱。我正在构建一个使用 Amazon.com 产品广告 API 的 Silverlight 应用程序。我想向我的应用程序添加身份验证,但我不想使用默认表单基本身份验证,而是想实现 OpenId。我看到许多使用雅虎或谷歌作为他们的供应商的景点。我确实记得至少一个场景,但不记得是哪个场景,它使用 Amazon.com 作为提供者。

如果有人能在这方面的文档上为我指出正确的方向,那就太好了。

编辑:我现在记得是 Target.com 允许您使用 Amazon.com 登录。

【问题讨论】:

  • 仅供参考,Target.com 由亚马逊服务实施和托管多年,因此共享帐户。我认为 Target 几个月前采用了他们自己的实现。

标签: silverlight-4.0 amazon openid-provider


【解决方案1】:

我对 OpenID 了解不多,但您几乎必须编写自定义身份验证服务,这还不错。 (顺便说一句,它仍然会利用实际上很方便的表单身份验证)

如果你知道如何通过代码验证.....

在服务器端,您需要三个部分。一个保存用户数据的类,一个继承自表单身份验证的类.. 以及一个处理登录异常的类..

这是一个服务器代码示例(抱歉减去了打开的 id 检查)

using System.ServiceModel.DomainServices.Server.ApplicationServices;

public class UserDTO : UserBase
{
    public string Email { get; set; }

    //Must be string since will be included in HTTP Headers
    public string Id { get; set; }

    public bool CanCreateSomething { get; set;}
}

使用系统; 使用 System.Data.Objects; 使用 System.ServiceModel.DomainServices.Hosting;

[EnableClientAccess]
public class CustomAuthenticationService : FormsAuthenticationService<UserDTO>
{


    protected override UserDTO ValidateCredentials(string name, string password, string customData,
                                                   out string userData)
    {
        UserDTO user = null;
        userData = null;

        OpenIDUser OIDusr;

        if OIDusr != null)
        {
            user = new UserDTO { Name = OIDusr.Description, Email = OIDusr.PrimaryEmail, Id= OIDusr.Id.ToString() };
        }

        if (user != null)
        {
            //Set custom data fields for HTTP session  
            userData = user.PartyId + ":" + user.Email;
        }


        return user;
    }

}

[Serializable]
public class FormsAuthenticationLogonException : Exception
{
    public FormsAuthenticationLogonException(string message) : base(message){}
}

public abstract class FormsAuthenticationService<TUser> : DomainService, IAuthentication<TUser>
    where TUser : UserBase
{
    #region IAuthentication<TUser> Members

    public TUser GetUser()
    {
        var currentUser = ServiceContext.User;
        if ((currentUser != null) && currentUser.Identity.IsAuthenticated)
        {
            var userIdentity = currentUser.Identity as FormsIdentity;
            if (userIdentity != null)
            {
                var ticket = userIdentity.Ticket;
                if (ticket != null)
                {
                    return GetCurrentUser(currentUser.Identity.Name, ticket.UserData);
                }
            }
        }
        return GetDefaultUser();
    }


    public TUser Login(string userName, string password, bool isPersistent, string customData)
    {
        string userData;
        TUser user = ValidateCredentials(userName, password, customData, out userData);
        if (user != null)
        {
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( /* version */
                1, userName, DateTime.Now, DateTime.Now.AddMinutes(30),
                isPersistent, userData, FormsAuthentication.FormsCookiePath);
            string encryptedTicket = FormsAuthentication.Encrypt(ticket);
            HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            HttpContextBase httpContext = (HttpContextBase) ServiceContext.GetService(typeof (HttpContextBase));
            httpContext.Response.Cookies.Add(authCookie);
        }
        else
        {
            HttpContextBase httpContext = (HttpContextBase) ServiceContext.GetService(typeof (HttpContextBase));
            httpContext.AddError(new FormsAuthenticationLogonException("Username or password is not correct."));
        }
        return user;
    }

    public TUser Logout()
    {
        FormsAuthentication.SignOut();
        return GetDefaultUser();
    }

    public void UpdateUser(TUser user)
    {
        throw new NotImplementedException();
    }

    #endregion

    protected abstract TUser GetCurrentUser(string name, string userData);

    protected virtual TUser GetDefaultUser()
    {
        return null;
    }

    protected abstract TUser ValidateCredentials(string name, string password, string customData,
                                                 out string userData);
}

在客户端.....

    LoginParameters loginParameters = new LoginParameters(UserName, Password);

        WebContextBase.Current.Authentication.Login(loginParameters, 
            delegate(LoginOperation operation)      
            {                     
                if (operation.HasError)    
                {
                    App.IsBusy = false;
                    operation.MarkErrorAsHandled();
                    UserName = string.Empty;
                    Password = string.Empty;
                    MessageBox.Show("Username or Password is incorrect!");
                    return;                 
                }

                //Login Success
                CustomAuthenticationContext authContext = new CustomAuthenticationContext();
                authContext.Load(authContext.GetUserQuery(), UserLoaded, false);
            }, null);

【讨论】:

    猜你喜欢
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 2018-08-26
    • 2017-01-10
    • 1970-01-01
    • 2017-10-14
    • 2020-09-17
    相关资源
    最近更新 更多