CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

First paste

108 Views
Copy Code Show/Hide Line Numbers
//-----------------------------------------------------------------------
// <copyright file="Util.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
 
namespace OpenIdProviderWebForms.Code {
    using System;
    using System.Web;
    using DotNetOpenAuth.OpenId;
    using DotNetOpenAuth.OpenId.Provider;
 
    public class Util {
        public static string ExtractUserName(Uri url) {
            return url.Segments[url.Segments.Length - 1];
        }
 
        public static string ExtractUserName(Identifier identifier) {
            return ExtractUserName(new Uri(identifier.ToString()));
        }
 
        public static Identifier BuildIdentityUrl() {
            return BuildIdentityUrl(HttpContext.Current.User.Identity.Name);
        }
 
        public static Identifier BuildIdentityUrl(string username) {
            // This sample Provider has a custom policy for normalizing URIs, which is that the whole
            // path of the URI be lowercase except for the first letter of the username.
            username = username.Substring(0, 1).ToUpperInvariant() + username.Substring(1).ToLowerInvariant();
            return new Uri(HttpContext.Current.Request.Url, HttpContext.Current.Response.ApplyAppPathModifier("~/user.aspx/" + username));
        }
 
        internal static void ProcessAuthenticationChallenge(IAuthenticationRequest idrequest) {
            if (idrequest.Immediate) {
                if (idrequest.IsDirectedIdentity) {
                    if (HttpContext.Current.User.Identity.IsAuthenticated) {
                        idrequest.LocalIdentifier = Util.BuildIdentityUrl();
                        idrequest.IsAuthenticated = true;
                    } else {
                        idrequest.IsAuthenticated = false;
                    }
                } else {
                    string userOwningOpenIdUrl = Util.ExtractUserName(idrequest.LocalIdentifier);
 
                    // NOTE: in a production provider site, you may want to only 
                    // respond affirmatively if the user has already authorized this consumer
                    // to know the answer.
                    idrequest.IsAuthenticated = userOwningOpenIdUrl == HttpContext.Current.User.Identity.Name;
                }
 
                if (idrequest.IsAuthenticated.Value) {
                    // add extension responses here.
                }
            } else {
                HttpContext.Current.Response.Redirect("~/decide.aspx", true);
            }
        }
 
        internal static void ProcessAnonymousRequest(IAnonymousRequest request) {
            if (request.Immediate) {
                // NOTE: in a production provider site, you may want to only
                // respond affirmatively if the user has already authorized this consumer
                // to know the answer.
                request.IsApproved = HttpContext.Current.User.Identity.IsAuthenticated;
 
                if (request.IsApproved.Value) {
                    // Add extension responses here.
                    // These would typically be filled in from a user database
                }
            } else {
                HttpContext.Current.Response.Redirect("~/decide.aspx", true);
            }
        }
    }
}
by Andreas Grech
  March 08, 2010 @ 12:33pm

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate