是否可以使用 Amazon.com 作为身份验证提供商

发布于 2024-08-27 18:15:56 字数 203 浏览 10 评论 0原文

我正在构建一个使用 Amazon.com 产品广告 API 的 Silverlight 应用程序。我想向我的应用程序添加身份验证,但我想实现 OpenId,而不是使用默认表单基本身份验证。

我看到许多网站使用雅虎或谷歌作为其提供商。我确实记得至少有一个网站 target.com 允许您使用 Amazon.com 登录。

有人可以指出我执行此身份验证的正确文档吗?

I'm building a Silverlight app that is consuming the Amazon.com product advertising API. I want to add authentication to my app, but instead of using the default forms base authentication, I would like to implement OpenId.

I see many websites that use Yahoo, or Google, for their provider. And I do remember at least one site, target.com, that allows you to use an Amazon.com login.

Can someone point me to the correct documentation to implement this authentication?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薔薇婲 2024-09-03 18:15:56

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

如果您知道如何通过代码进行验证......

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

的服务器代码示例(抱歉,减去了 open 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; ;
使用 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);

I don't know much about OpenID but you pretty much have to write a custom authenticatin service, which isn't that bad. (by the way it will still leverage forms authentication which is actually convienent)

If you know how to validate via code .....

On the server side you need three pieces. a class to hold your user data, a class that inherits from forms auth.. and a class that handles the logon exceptions..

here is an example of the server code (sorry minus the open id check)

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

using System;
using System.Data.Objects;
using 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);
}

On the client side .....

    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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文