容易openid? .NET / JS?

时间:2010-12-20 01:44:39

标签: javascript .net openid

我试图理解dotnetopenid但失败了。我不太了解asp.net,我想这样做有问题。

是否有一个简单的JS openid lib?或者为C#桌面应用程序执行它的简单/好的openid示例?

2 个答案:

答案 0 :(得分:1)

以下是c#.net中开放ID的示例。不是桌面而是网络。

http://www.nikhedonia.com/notebook/entry/openid-and-asp-net-mvc/

答案 1 :(得分:0)

这是我的aspx文件,我只添加了一行,即WebApplication1.openidtest.Authenticate行

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <% WebApplication1.openidtest.Authenticate("http://your_id.myopenid.com/"); %>
    </div>
    </form>
</body>
</html>

这是cs文件。注意它可以工作,但不会处理错误之类的事情,而且只是用于测试/断点。

注意:如果电子邮件需要并且用户提交的电子邮件没有电子邮件,则不会列出电子邮件,因此您必须相应地进行检查和处理。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using DotNetOpenAuth;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
namespace WebApplication1
{
    static public class openidtest
    {
        static public void Authenticate(string openIdIdentifier)
        {
            var openId = new OpenIdRelyingParty();
            var response = openId.GetResponse();
            if (UserNeedsToLogin(response))
            {
                var request = openId.CreateRequest(openIdIdentifier);
                request.AddExtension(new ClaimsRequest { Email = DemandLevel.Require });
                request.RedirectingResponse.Send();
                return;
            }

            HandleAuthenticationResponse(response);
        }

        static bool UserNeedsToLogin(IAuthenticationResponse response)
        {
            return response == null;
        }

        static void HandleAuthenticationResponse(IAuthenticationResponse response)
        {
            switch (response.Status)
            {
                case AuthenticationStatus.Authenticated:
                    {
                        var claims = response.GetExtension<ClaimsResponse>();
                        if (claims != null)
                        {
                            var s = claims.Email;
                        }
                        return;
                        //return HandleSuccessfulLogin(response);
                    }
                case AuthenticationStatus.Canceled:
                    //_context.ErrorMessage = "Login was cancelled at the provider.";
                    break;
                case AuthenticationStatus.Failed:
                    //_context.ErrorMessage = "Login failed at the provider.";
                    break;
                case AuthenticationStatus.SetupRequired:
                    //_context.ErrorMessage = "The provider requires setup.";
                    break;
                default:
                    //_context.ErrorMessage = "Login failed.";
                    break;
            }
        }
    }
}