public void RespondNull()
 {
     var req = new AttributeRequest();
     req.TypeUri = "http://someType";
     req.Count = 1;
     req.Respond(null);
 }
Beispiel #2
0
        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">
        /// The <paramref name="obj"/> parameter is null.
        /// </exception>
        public override bool Equals(object obj)
        {
            AttributeRequest other = obj as AttributeRequest;

            if (other == null)
            {
                return(false);
            }

            if (this.TypeUri != other.TypeUri)
            {
                return(false);
            }

            if (this.Count != other.Count)
            {
                return(false);
            }

            if (this.IsRequired != other.IsRequired)
            {
                return(false);
            }

            return(true);
        }
 public void CtorFull()
 {
     var req = new AttributeRequest(WellKnownAttributes.Contact.Email, true, 5);
     Assert.AreEqual(WellKnownAttributes.Contact.Email, req.TypeUri);
     Assert.IsTrue(req.IsRequired);
     Assert.AreEqual(5, req.Count);
 }
 public void CtorDefault()
 {
     AttributeRequest req = new AttributeRequest();
     Assert.AreEqual(1, req.Count);
     Assert.IsNull(req.TypeUri);
     Assert.IsFalse(req.IsRequired);
 }
		public void RespondSimpleValue() {
			var req = new AttributeRequest();
			req.TypeUri = "http://someType";
			var resp = req.Respond("value");
			Assert.AreEqual(req.TypeUri, resp.TypeUri);
			Assert.AreEqual(1, resp.Values.Count);
			Assert.AreEqual("value", resp.Values[0]);
		}
		public void RespondTwoValues() {
			var req = new AttributeRequest();
			req.TypeUri = "http://someType";
			req.Count = 2;
			var resp = req.Respond("value1", "value2");
			Assert.AreEqual(req.TypeUri, resp.TypeUri);
			Assert.AreEqual(2, resp.Values.Count);
			Assert.AreEqual("value1", resp.Values[0]);
			Assert.AreEqual("value2", resp.Values[1]);
		}
        public ActionResult Authenticate(string returnUrl)
        {
            var openid = new OpenIdRelyingParty();
            var response = openid.GetResponse();

            if (response == null) {
                // Stage 2: user submitting Identifier
                Identifier id;
                if (Identifier.TryParse(Request.Form["openid_identifier"], out id)) {
                    try {
                        var req = openid.CreateRequest(id);
                        var fetch = new FetchRequest();
                        //ask for more info - the email address
                        //no guarantee you'll get it back :)
                        var email = new AttributeRequest(WellKnownAttributes.Contact.Email);
                        email.IsRequired = true;
                        fetch.Attributes.Add(email);
                        req.AddExtension(fetch);

                        return req.RedirectingResponse.AsActionResult();

                    } catch (ProtocolException ex) {
                        ViewData["Message"] = ex.Message;
                        return View("Login");
                    }
                } else {
                    ViewData["Message"] = "Invalid identifier";
                    return View("Login");
                }
            } else {
                // Stage 3: OpenID Provider sending assertion response
                switch (response.Status) {
                    case AuthenticationStatus.Authenticated:
                        //They're in there...

                        var fetch = response.GetExtension<FetchResponse>();
                        string email = null;
                        if (fetch != null) {
                            IList<string> emailAddresses = fetch.Attributes[WellKnownAttributes.Contact.Email].Values;
                            email = emailAddresses.Count > 0 ? emailAddresses[0] : null;

                        }
                        var friendly = email ?? response.FriendlyIdentifierForDisplay;
                        return AuthAndRedirect(friendly, response.ClaimedIdentifier);
                    case AuthenticationStatus.Canceled:
                        ViewData["Message"] = "Canceled at provider";
                        return View("Login");
                    case AuthenticationStatus.Failed:
                        ViewData["Message"] = response.Exception.Message;
                        return View("Login");
                }
            }
            return new EmptyResult();
        }
		public void EqualityTests() {
			var req1 = new AttributeRequest();
			var req2 = new AttributeRequest();
			Assert.AreEqual(req1, req2);

			req1.IsRequired = true;
			Assert.AreNotEqual(req1, req2);
			req2.IsRequired = true;
			Assert.AreEqual(req1, req2);

			req1.Count = 3;
			Assert.AreNotEqual(req1, req2);
			req2.Count = 3;
			Assert.AreEqual(req1, req2);

			req1.TypeUri = "http://hi";
			Assert.AreNotEqual(req1, req2);
			req2.TypeUri = "http://hi";
			Assert.AreEqual(req1, req2);
		}
		public void SetCountNegative() {
			var req = new AttributeRequest();
			req.Count = -1;
		}
		public void SetCountZero() {
			var req = new AttributeRequest();
			req.Count = 0;
		}
Beispiel #11
0
/// <summary>
/// Called when the message has been received,
/// after it passes through the channel binding elements.
/// </summary>
void IMessageWithEvents.OnReceiving()
{
    var extraData = ((IMessage)this).ExtraData;
            var requiredAliases = ParseAliasList(this.RequiredAliases);
            var optionalAliases = ParseAliasList(this.OptionalAliases);

            // if an alias shows up in both lists, an exception will result implicitly.
            var allAliases = new List<string>(requiredAliases.Count + optionalAliases.Count);
            allAliases.AddRange(requiredAliases);
            allAliases.AddRange(optionalAliases);
            if (allAliases.Count == 0) {
                Logger.OpenId.Error("Attribute Exchange extension did not provide any aliases in the if_available or required lists.");
                return;
            }

            AliasManager aliasManager = new AliasManager();
            foreach (var alias in allAliases) {
                string attributeTypeUri;
                if (extraData.TryGetValue("type." + alias, out attributeTypeUri)) {
                    aliasManager.SetAlias(alias, attributeTypeUri);
                    AttributeRequest att = new AttributeRequest {
                        TypeUri = attributeTypeUri,
                        IsRequired = requiredAliases.Contains(alias),
                    };
                    string countString;
                    if (extraData.TryGetValue("count." + alias, out countString)) {
                        if (countString == "unlimited") {
                            att.Count = int.MaxValue;
                        } else {
                            int count;
                            if (int.TryParse(countString, out count) && count > 0) {
                                att.Count = count;
                            } else {
                                Logger.OpenId.Error("count." + alias + " could not be parsed into a positive integer.");
                            }
                        }
                    } else {
                        att.Count = 1;
                    }
                    this.Attributes.Add(att);
                } else {
                    Logger.OpenId.Error("Type URI definition of alias " + alias + " is missing.");
                }
            }
}
		public void RespondTooManyValues() {
			var req = new AttributeRequest();
			req.TypeUri = "http://someType";
			req.Count = 1;
			req.Respond("value1", "value2");
		}
        public ActionResult OpenIdLogin()
        {
            string returnUrl = VirtualPathUtility.ToAbsolute("~/");
            var openid = new OpenIdRelyingParty();
            var response = openid.GetResponse();
            if (response == null) {
                // Stage 2: user submitting Identifier
                Identifier id;
                if (Identifier.TryParse(Request["openid_identifier"], out id)) {
                    try {
                        IAuthenticationRequest req = openid.CreateRequest(Request["openid_identifier"]);

                        var fetch = new FetchRequest();
                        //ask for more info - the email address
                        var item = new AttributeRequest(WellKnownAttributes.Contact.Email);
                        item.IsRequired=true;
                        fetch.Attributes.Add(item);
                        req.AddExtension(fetch);

                        return req.RedirectingResponse.AsActionResult();
                    } catch (ProtocolException ex) {
                        ViewData["Message"] = ex.Message;
                        return View("Logon");
                    }
                } else {
                    ViewData["Message"] = "Invalid identifier";
                    return View("Logon");
                }
            } else {
                // Stage 3: OpenID Provider sending assertion response
                switch (response.Status) {
                    case AuthenticationStatus.Authenticated:

                        var fetch = response.GetExtension<FetchResponse>();
                        string name = response.FriendlyIdentifierForDisplay;
                        if (fetch != null) {
                            IList<string> emailAddresses = fetch.Attributes[WellKnownAttributes.Contact.Email].Values;
                            string email = emailAddresses.Count > 0 ? emailAddresses[0] : null;
                            //don't show the email - it's creepy. Just use the name of the email
                            name = email.Substring(0, email.IndexOf('@'));
                        } else {

                            name = name.Substring(0, name.IndexOf('.'));
                        }

                        FormsAuthentication.SetAuthCookie(name, false);

                        if (!string.IsNullOrEmpty(returnUrl)) {
                            return Redirect(returnUrl);
                        } else {
                            return RedirectToAction("Index", "Home");
                        }
                    case AuthenticationStatus.Canceled:
                        ViewData["Message"] = "Canceled at provider";
                        return View("Logon");
                    case AuthenticationStatus.Failed:
                        ViewData["Message"] = response.Exception.Message;
                        return View("Logon");
                }
            }
            return new EmptyResult();
        }
Beispiel #14
0
        /// <summary>
        /// Called when the message has been received,
        /// after it passes through the channel binding elements.
        /// </summary>
        void IMessageWithEvents.OnReceiving()
        {
            var extraData       = ((IMessage)this).ExtraData;
            var requiredAliases = ParseAliasList(this.RequiredAliases);
            var optionalAliases = ParseAliasList(this.OptionalAliases);

            // if an alias shows up in both lists, an exception will result implicitly.
            var allAliases = new List <string>(requiredAliases.Count + optionalAliases.Count);

            allAliases.AddRange(requiredAliases);
            allAliases.AddRange(optionalAliases);
            if (allAliases.Count == 0)
            {
                Logger.OpenId.Error("Attribute Exchange extension did not provide any aliases in the if_available or required lists.");
                return;
            }

            AliasManager aliasManager = new AliasManager();

            foreach (var alias in allAliases)
            {
                string attributeTypeUri;
                if (extraData.TryGetValue("type." + alias, out attributeTypeUri))
                {
                    aliasManager.SetAlias(alias, attributeTypeUri);
                    AttributeRequest att = new AttributeRequest {
                        TypeUri    = attributeTypeUri,
                        IsRequired = requiredAliases.Contains(alias),
                    };
                    string countString;
                    if (extraData.TryGetValue("count." + alias, out countString))
                    {
                        if (countString == "unlimited")
                        {
                            att.Count = int.MaxValue;
                        }
                        else
                        {
                            int count;
                            if (int.TryParse(countString, out count) && count > 0)
                            {
                                att.Count = count;
                            }
                            else
                            {
                                Logger.OpenId.Error("count." + alias + " could not be parsed into a positive integer.");
                            }
                        }
                    }
                    else
                    {
                        att.Count = 1;
                    }
                    this.Attributes.Add(att);
                }
                else
                {
                    Logger.OpenId.Error("Type URI definition of alias " + alias + " is missing.");
                }
            }
        }
 /// <summary>
 /// Adds a request for the values of a given attribute.
 /// Applicable to Relying Parties.
 /// </summary>
 /// <param name="attribute">The attribute.</param>
 public void AddAttribute(AttributeRequest attribute)
 {
     ErrorUtilities.VerifyArgumentNotNull(attribute, "attribute");
     ErrorUtilities.VerifyArgumentNamed(!this.ContainsAttribute(attribute.TypeUri), "attribute", OpenIdStrings.AttributeAlreadyAdded, attribute.TypeUri);
     this.attributesRequested.Add(attribute);
 }