使用Nemiro OAuth库在Webforms中进行Google身份验证 [英] Using Nemiro OAuth library for Google authentication in Webforms

查看:119
本文介绍了使用Nemiro OAuth库在Webforms中进行Google身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Nemiro库在Webforms asp.net项目中针对Google进行身份验证.该库文档位于 Nemiro GoogleClient文档

I am trying to use Nemiro library to authenticate against Google in a Webforms asp.net project. This library documentation is at Nemiro GoogleClient Documenation

我有一个名为ExternalLogin.aspx的简单aspx页面,其标记和代码隐藏如下.

I have a simple aspx page called ExternalLogin.aspx, whose markup and code-behind are as given below.

问题

使用我拥有的代码,当单击Login using Google按钮时,用户不会被定向到Google的授权页面.我的代码中缺少什么原因?

With code that I have, when Login using Google button is clicked, then user does not get directed to Google's authorization page. What is missing in my code that is causing this?

标记

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExternalLogin.aspx.cs" Inherits="ExternalLogin" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnGoogle" runat="server" Text="Login using Google" OnClick="btnGoogle_Click" />
        </div>
    </form>
</body>
</html>

代码隐藏

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

using Nemiro;
using Nemiro.OAuth.Clients;
using Nemiro.OAuth;

public partial class ExternalLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnGoogle_Click(object sender, EventArgs e)
    {

        var result = OAuthWeb.VerifyAuthorization();
        if (result.IsSuccessfully)
        {
            var user = result.UserInfo;
            Response.Write(String.Format("User ID:  {0}<br />", user.UserId));
            Response.Write(String.Format("Name:     {0}<br />", user.DisplayName));
            Response.Write(String.Format("Email:    {0}", user.Email));
        }
    }
}

我还在Application_Start事件中定义了Google OAuth的密钥,如下所示.

I have also defined the keys for Google OAuth in Application_Start event as below.

void Application_Start(object sender, EventArgs e)
{
     Nemiro.OAuth.OAuthManager.RegisterClient(
              new Nemiro.OAuth.Clients.GoogleClient(
                 "some-value-1",
                 "some-value-2"
               ));
}

推荐答案

我认为您应该查看OAuthWeb.RedirectToAuthorization方法.这是 API文档供您参考.因此,只需在btnGoogle_Click中调用此方法,然后在Page_Load事件处理程序中验证您的授权即可.

I think you should be looking at OAuthWeb.RedirectToAuthorization method. Here's the API doc for your reference. So just call this method in your btnGoogle_Click, and then verify your authorization in Page_Load event handler.

这是示例代码:

protected void btnGoogle_Click(object sender, EventArgs e)
{
    OAuthWeb.RedirectToAuthorization("Google", new Uri(Request.Url, "ExternalLogin.aspx").AbsoluteUri);
}

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostback)
   {
        var result = OAuthWeb.VerifyAuthorization();
        if (result.IsSuccessfully)
        {
            var user = result.UserInfo;
            Response.Write(String.Format("User ID:  {0}<br />", user.UserId));
            Response.Write(String.Format("Name:     {0}<br />", user.DisplayName));
            Response.Write(String.Format("Email:    {0}", user.Email));
        }
    }
}

此外,如果您想在其他页面上验证授权结果,只需在URI构造函数中更改页面名称,然后将验证代码放在新页面的Page_Load事件中即可.

Also, if you'd like to verify the authorization results on a different page, just change the page name in the URI constructor, and put verification code in the Page_Load event of your new page.

希望有帮助.

这篇关于使用Nemiro OAuth库在Webforms中进行Google身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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