From 338dc70a0e17bb6a557862c41f9106b955fdc0b9 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Mon, 21 Jan 2008 05:50:43 +0000 Subject: [PATCH] OpenIdTextBox now catches some failure exceptions and fires the Error event as appropriate. Fixes issue 19. git-svn-id: https://dotnetopenid.googlecode.com/svn/trunk@78 01efa1a6-402a-0410-b0ae-47b76eba00f0 --- .../login.aspx.cs | 7 +- .../Janrain.OpenId/Consumer/OpenIdTextBox.cs | 65 ++++++++++--------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/source/JanRain.OpenID.ConsumerPortal/login.aspx.cs b/source/JanRain.OpenID.ConsumerPortal/login.aspx.cs index 6cff0c28e6..117961bffd 100644 --- a/source/JanRain.OpenID.ConsumerPortal/login.aspx.cs +++ b/source/JanRain.OpenID.ConsumerPortal/login.aspx.cs @@ -15,8 +15,6 @@ public partial class login : System.Web.UI.Page { - - /// /// Handles the Load event of the Page control. /// @@ -34,12 +32,13 @@ protected void Page_Load(object sender, EventArgs e) /// /// protected void OpenIdLogin1_LoggedIn(object sender, NerdBank.OpenId.Consumer.OpenIdTextBox.OpenIdEventArgs e) - { - State.ProfileFields = e.ProfileFields; + { + State.ProfileFields = e.ProfileFields; } protected void OpenIdLogin1_Error(object sender, NerdBank.OpenId.Consumer.OpenIdTextBox.ErrorEventArgs e) { loginFailedLabel.Visible = true; + loginFailedLabel.Text += ": " + e.ErrorMessage; } protected void OpenIdLogin1_Canceled(object sender, NerdBank.OpenId.Consumer.OpenIdTextBox.ErrorEventArgs e) { diff --git a/source/Janrain.OpenId/Consumer/OpenIdTextBox.cs b/source/Janrain.OpenId/Consumer/OpenIdTextBox.cs index bc2ee777f2..176ae1db69 100644 --- a/source/Janrain.OpenId/Consumer/OpenIdTextBox.cs +++ b/source/Janrain.OpenId/Consumer/OpenIdTextBox.cs @@ -24,6 +24,7 @@ using Janrain.OpenId.Session; using Janrain.OpenId.RegistrationExtension; using Janrain.OpenId.Store; +using System.Net; namespace NerdBank.OpenId.RegistrationExtension { @@ -336,37 +337,43 @@ public void Login() if (string.IsNullOrEmpty(Text)) throw new InvalidOperationException(Janrain.OpenId.Strings.OpenIdTextBoxEmpty); - Janrain.OpenId.Consumer.Consumer consumer = - new Janrain.OpenId.Consumer.Consumer(new SystemHttpSessionState(Page.Session), MemoryStore.GetInstance()); - - Uri userUri = UriUtil.NormalizeUri(Text); - // Initiate openid request - AuthRequest request = consumer.Begin(userUri); - if (EnableRequestProfile) addProfileArgs(request); - - // Build the trust root - UriBuilder builder = new UriBuilder(Page.Request.Url.AbsoluteUri); - builder.Query = null; - builder.Password = null; - builder.UserName = null; - builder.Fragment = null; - builder.Path = Page.Request.ApplicationPath; - string trustRoot = builder.Uri.ToString(); - - // Build the return_to URL - UriBuilder return_to = new UriBuilder(Page.Request.Url); - // Trim off any old "openid." prefixed parameters to avoid carrying - // state from a prior login attempt. - return_to.Query = string.Empty; - NameValueCollection return_to_params = new NameValueCollection(Page.Request.QueryString.Count); - foreach (string key in Page.Request.QueryString) { - if (!key.StartsWith(QueryStringArgs.openid.Prefix) && key != QueryStringArgs.nonce) { - return_to_params.Add(key, Page.Request.QueryString[key]); + try { + Janrain.OpenId.Consumer.Consumer consumer = + new Janrain.OpenId.Consumer.Consumer(new SystemHttpSessionState(Page.Session), MemoryStore.GetInstance()); + + Uri userUri = UriUtil.NormalizeUri(Text); + // Initiate openid request + AuthRequest request = consumer.Begin(userUri); + if (EnableRequestProfile) addProfileArgs(request); + + // Build the trust root + UriBuilder builder = new UriBuilder(Page.Request.Url.AbsoluteUri); + builder.Query = null; + builder.Password = null; + builder.UserName = null; + builder.Fragment = null; + builder.Path = Page.Request.ApplicationPath; + string trustRoot = builder.Uri.ToString(); + + // Build the return_to URL + UriBuilder return_to = new UriBuilder(Page.Request.Url); + // Trim off any old "openid." prefixed parameters to avoid carrying + // state from a prior login attempt. + return_to.Query = string.Empty; + NameValueCollection return_to_params = new NameValueCollection(Page.Request.QueryString.Count); + foreach (string key in Page.Request.QueryString) { + if (!key.StartsWith(QueryStringArgs.openid.Prefix) && key != QueryStringArgs.nonce) { + return_to_params.Add(key, Page.Request.QueryString[key]); + } } + UriUtil.AppendQueryArgs(return_to, return_to_params); + Uri redirectUrl = request.CreateRedirect(trustRoot, return_to.Uri, AuthRequest.Mode.SETUP); + Page.Response.Redirect(redirectUrl.AbsoluteUri); + } catch (WebException ex) { + OnError(ex); + } catch (FailureException ex) { + OnError(ex); } - UriUtil.AppendQueryArgs(return_to, return_to_params); - Uri redirectUrl = request.CreateRedirect(trustRoot, return_to.Uri, AuthRequest.Mode.SETUP); - Page.Response.Redirect(redirectUrl.AbsoluteUri); } void addProfileArgs(AuthRequest request)