/// <summary>
        ///
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        public UserIdentifier(string id, UserIdentifierType type)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException("id");
            }

            this.Id   = id;
            this.Type = type;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        public UserIdentifier(string id, UserIdentifierType type)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException("id");
            }

            this.Id = id;
            this.Type = type;
        }
Exemplo n.º 3
0
        public async Task MsAppRedirectUriTest()
        {
            Sts sts = new AadSts();
            AuthenticationContext context = await AuthenticationContext.CreateAsync(sts.Authority);

            try
            {
                UserIdentifierType t = UserIdentifierType.RequiredDisplayableId;
                context.AcquireTokenAndContinue(sts.ValidResource, sts.ValidClientId, new Uri("ms-app://test/"), null);

                Verify.Fail("Argument exception expected");
            }
            catch (AdalException ex)
            {
                Verify.AreEqual(ex.ErrorCode, Sts.AuthenticationUiFailedError);
                Verify.IsTrue(ex.InnerException is ArgumentException);
            }

            try
            {
                WebAuthenticationBroker.GetCurrentApplicationCallbackUri();

                Verify.Fail("Exception expected");
            }
            catch (Exception ex)
            {
                Verify.IsTrue(ex.Message.Contains("hostname"));
            }

            try
            {
                context.AcquireTokenAndContinue(sts.ValidResource, sts.ValidClientId, null, null);

                Verify.Fail("Exception expected");
            }
            catch (AdalException ex)
            {
                Verify.AreEqual(ex.ErrorCode, "need_to_set_callback_uri_as_local_setting");
            }

            try
            {
                // Incorrect ms-app
                ApplicationData.Current.LocalSettings.Values["CurrentApplicationCallbackUri"] = "ms-app://s-1-15-2-2097830667-3131301884-2920402518-3338703368-1480782779-4157212157-3811015497/";
                context.AcquireTokenAndContinue(sts.ValidResource, sts.ValidClientId, null, null);

                Verify.Fail("Exception expected");
            }
            catch (AdalException ex)
            {
                Verify.AreEqual(ex.ErrorCode, Sts.AuthenticationUiFailedError);
            }
        }
        public static async Task <string> AcquireToken(Dictionary <string, string> input)
        {
            Dictionary <string, object> res = new Dictionary <string, object>();
            AuthenticationContext       ctx = new AuthenticationContext(input["authority"]);

            try
            {
                AuthenticationResult result = null;

                if (!input.ContainsKey("redirect_uri"))
                {
                    UserCredential userCred = new UserCredential();
                    result = await ctx.AcquireTokenAsync(input["resource"], input["client_id"], userCred).ConfigureAwait(false);
                }
                else if (input.ContainsKey("user_identifier") && input.ContainsKey("password"))
                {
                    UserPasswordCredential user = new UserPasswordCredential(input["user_identifier"], input["password"]);
                    result = await ctx.AcquireTokenAsync(input["resource"], input["client_id"], user).ConfigureAwait(false);
                }
                else if (input.ContainsKey("user_identifier") && input.ContainsKey("user_identifier_type"))
                {
                    UserIdentifierType userIdentifierType;
                    UserIdentifierType.TryParse(input["user_identifier_type"], out userIdentifierType);
                    string prompt = input.ContainsKey("prompt_behavior") ? input["prompt_behavior"] : null;
                    result = await ctx.AcquireTokenAsync(input["resource"], input["client_id"], new Uri(input["redirect_uri"]),
                                                         GetPlatformParametersInstance(prompt),
                                                         new UserIdentifier(input["user_identifier"], userIdentifierType))
                             .ConfigureAwait(false);
                }
                else
                {
                    string prompt = input.ContainsKey("prompt_behavior") ? input["prompt_behavior"] : null;
                    result = await ctx.AcquireTokenAsync(input["resource"], input["client_id"], new Uri(input["redirect_uri"]),
                                                         GetPlatformParametersInstance(prompt)).ConfigureAwait(false);
                }
                res = ProcessResult(result, input);
            }
            catch (Exception exc)
            {
                res.Add("error", exc.Message);
            }
            return(FromDictionaryToJson(res));
        }
Exemplo n.º 5
0
        public static async Task <string> AcquireToken(Dictionary <string, string> input)
        {
            Dictionary <string, object> res = new Dictionary <string, object>();
            AuthenticationContext       ctx = new AuthenticationContext(input["authority"]);

            try
            {
                AuthenticationResult result = null;

                if (!input.ContainsKey("redirect_uri"))
                {
                    UserCredential userCred = new UserCredential();
                    result = await ctx.AcquireTokenAsync(input["resource"], input["client_id"], userCred).ConfigureAwait(false);
                }
                else if (input.ContainsKey("user_identifier") && input.ContainsKey("password"))
                {
                    UserPasswordCredential user = new UserPasswordCredential(input["user_identifier"], input["password"]);
                    result = await ctx.AcquireTokenAsync(input["resource"], input["client_id"], user).ConfigureAwait(false);
                }
                else if (input.ContainsKey("user_identifier") && input.ContainsKey("user_identifier_type"))
                {
                    // user identifier type defaults to RequiredDisplayableId
                    UserIdentifierType userIdentifierType = UserIdentifierType.RequiredDisplayableId;
                    if (string.Equals(input["user_identifier_type"], "unique_id",
                                      StringComparison.InvariantCultureIgnoreCase))
                    {
                        userIdentifierType = UserIdentifierType.UniqueId;
                    }
                    else if (string.Equals(input["user_identifier_type"], "optional_displayable",
                                           StringComparison.InvariantCultureIgnoreCase))
                    {
                        userIdentifierType = UserIdentifierType.OptionalDisplayableId;
                    }
                    else if (string.Equals(input["user_identifier_type"], "required_displayable",
                                           StringComparison.InvariantCultureIgnoreCase))
                    {
                        userIdentifierType = UserIdentifierType.RequiredDisplayableId;
                    }

                    string prompt = input.ContainsKey("prompt_behavior") ? input["prompt_behavior"] : null;

                    if (input.ContainsKey("claims"))
                    {
                        result = await ctx.AcquireTokenAsync(input["resource"], input["client_id"], new Uri(input["redirect_uri"]),
                                                             GetPlatformParametersInstance(prompt),
                                                             new UserIdentifier(input["user_identifier"], userIdentifierType), null, input["claims"])
                                 .ConfigureAwait(false);
                    }
                    else
                    {
                        result = await ctx.AcquireTokenAsync(input["resource"], input["client_id"], new Uri(input["redirect_uri"]),
                                                             GetPlatformParametersInstance(prompt),
                                                             new UserIdentifier(input["user_identifier"], userIdentifierType))
                                 .ConfigureAwait(false);
                    }
                }
                else
                {
                    string prompt = input.ContainsKey("prompt_behavior") ? input["prompt_behavior"] : null;
                    result = await ctx.AcquireTokenAsync(input["resource"], input["client_id"], new Uri(input["redirect_uri"]),
                                                         GetPlatformParametersInstance(prompt)).ConfigureAwait(false);
                }
                res = ProcessResult(result, input);
            }
            catch (Exception exc)
            {
                res.Add("error", exc.Message);
            }
            return(FromDictionaryToJson(res));
        }