Using Nemiro OAuth library for Google authentication in Webforms

…衆ロ難τιáo~ 提交于 2019-12-11 14:55:38

问题


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

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

Question

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?

Markup

<%@ 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>

Code-behind

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

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

回答1:


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.

Here's the sample code:

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

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.

Hope it helps.



来源:https://stackoverflow.com/questions/49465126/using-nemiro-oauth-library-for-google-authentication-in-webforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!