如何使用DotNetOpenAuth登录到网站? [英] How to use DotNetOpenAuth to login to websites?

查看:609
本文介绍了如何使用DotNetOpenAuth登录到网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是,如果用户登录到Gmail中,如果他们去我的网站,他们会自动得到登录。

我按以下方式做这件事......也许有这样做的更好的方法。

在我的网站我有使用的地方,所以我知道的网站注册用户的贾米勒地址给他们的Gmail地址。

所以,当他们去我的网站,我想知道他们是否已经登录到Gmail和什么是他们的Gmail地址。

我应该如何使用DotNetOpenAuth找到这些信息?

我发现下面code从网络,它是验证用户。但我不得不preSS的按钮,转到Gmail登录每次。
如果用户已经在使用Gmail我没有要求用户进行登录,我可以使用它。
我如何修改此code,以实现这一目标?

 静态字符串openidurl =htt​​ps://www.google.com/accounts/o8/id;
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    //响应
     OpenIdRelyingParty的OpenID =新OpenIdRelyingParty();     变种响应= openid.GetResponse();
     如果(响应!= NULL)
     {
             开关(response.Status)
             {
                 案例AuthenticationStatus.Authenticated:                 VAR取= response.GetExtension< FetchResponse>();
                 字符串email =;
                 如果(取!= NULL)
                 {
                     电子邮件= fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
                 }
                打破;
              }
    }
}
保护无效的button1_Click(对象发件人,EventArgs的发送)
{
    使用(OpenIdRelyingParty的OpenID =新OpenIdRelyingParty())
    {
        IAuthenticationRequest请求= openid.CreateRequest(openidurl);        VAR取=新FetchRequest();
        fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
        request.AddExtension(读取);        //发送你的访问者提供他们进行身份验证。
        request.RedirectToProvider();
    }
}


解决方案

这听起来像你问的是什么单点登录,其中一个站点访问者是谁已经登录到谷歌被记到您的网站时,他们首先访问它,而不是点击网站上的谷歌登录按钮后。

简短的回答是,你不能做到这一点。较长的答案是,你可以亲近。

第一和硬限制是第一次访问您的网站将永远不会自动登录,因为谷歌和用户还不信任你的网站。每个用户都有明确登录一次,与谷歌询问用户你要登录这个网站,记住这个选择吗?如果他们说是,那么在未来,当用户已经登录到谷歌和访问您的网站,他们可以点击网站上的谷歌登录按钮,他们永远不会看到谷歌 - 他们就会立即登录到您的站点。

因此​​,接下来的问题是,你如何删除用户的要求,点击登录谷歌。您可以通过未认证用户访问你的网站,你可以立即将其重定向到登录页面,将立即启动谷歌登录流(即 OpenIdRelyingParty.CreateRequest(谷歌)完成这个.RedirectToProvider ()电话),采用直接模式。如果用户没有登录到谷歌这将失败并信任你的网站,但影响将是用户不会看到谷歌登录屏幕,如果他们的的信任你的网站,但会宁立即登录。

I want to do is, if the users are logged into gmail and if they go to my website they automatically get logged in.

I am doing it in the following way... maybe there is a better way of doing it.

In my website I have a place for uses to give their gmail address so my website knows gamil address of the registered user.

So when they go to my website I want to know whether they are logged into gmail and what is their gmail address.

How should I find this information using DotNetOpenAuth?

I found following code from the web and it is authenticating the user. But i have to press the button and go to gmail login every time. if the user is already using gmail I don’t have to ask the user for login i can use it. How do i modify this code to achieve that?

static string openidurl = "https://www.google.com/accounts/o8/id";
protected void Page_Load(object sender, EventArgs e)
{
    //The Response
     OpenIdRelyingParty openid = new OpenIdRelyingParty();

     var response = openid.GetResponse();
     if (response != null)
     {
             switch (response.Status)
             {
                 case AuthenticationStatus.Authenticated:

                 var fetch = response.GetExtension<FetchResponse>();
                 string email = ""; 
                 if (fetch != null)
                 {
                     email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
                 }  
                break;
              }
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    using (OpenIdRelyingParty openid = new OpenIdRelyingParty())
    {
        IAuthenticationRequest request = openid.CreateRequest(openidurl);

        var fetch = new FetchRequest();
        fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
        request.AddExtension(fetch);

        // Send your visitor to their Provider for authentication.
        request.RedirectToProvider();
    }
}

解决方案

It sounds like what you're asking for is "single-sign-on", where a visitor to your site who is already logged into Google is immediately logged into your site when they first visit it, rather than after clicking a "Google Login" button on your site.

The short answer is you can't do this. The longer answer is that you can get close.

The first and hard restriction is that first-time visitors to your site will never get automatically signed in, because Google and the user don't yet trust your site. Every user has to explicitly log in once, with Google asking the user "do you want to log into this site and remember this choice?" If they say yes, then in the future when the user is already logged into Google and visits your site, they can click the Google Login button on your site and they'll never see Google -- they'll just be immediately logged into your site.

So the next question is how do you remove the requirement on the user to click "google Login". You can accomplish this by when an unauthenticated user visits your site, you can immediately redirect them to your log in page, which will immediately initiate the "Google Login" flow (the OpenIdRelyingParty.CreateRequest(google).RedirectToProvider() call), using "immediate mode". This will fail if the user isn't logged into Google and trust your site, but the impact will be the user won't see a Google login screen if they do trust your site, but will rather be immediately logged in.

这篇关于如何使用DotNetOpenAuth登录到网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆