Ejemplo n.º 1
0
        public static void Main()
        {
            var auth = new AuthorizationManager();

            //LOGIN
            var userToken = auth.Authorize("mpavelek", "123");

            //LOGOUT
            auth.SignOut(userToken);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create auth button
        /// </summary>
        /// <param name="htmlHelper">Html helper</param>
        /// <param name="options">Button options</param>
        /// <returns>Return button html content</returns>
        public static IHtmlContent AuthButton(this IHtmlHelper htmlHelper, AuthButtonOptions options)
        {
            if (options == null || (options.UseNowVerifyResult && !options.AllowAccess && options.ForbidStyle != ForbidStyle.Disable))
            {
                return(HtmlString.Empty);
            }
            bool allowAccess = true;

            if (!options.UseNowVerifyResult)
            {
                allowAccess = AuthorizationManager.Authorize(new AuthorizeOptions()
                {
                    Action        = options.ActionOptions?.Action,
                    Controller    = options.ActionOptions?.Controller,
                    Area          = options.ActionOptions?.Area,
                    Application   = ApplicationManager.Current,
                    Claims        = HttpContextHelper.Current.User.Claims.ToDictionary(c => c.Type, c => c.Value),
                    Method        = options.ActionOptions?.Method,
                    ActionContext = htmlHelper?.ViewContext
                })?.AllowAccess ?? false;
            }
            if (!allowAccess && options.ForbidStyle != ForbidStyle.Disable)
            {
                return(HtmlString.Empty);
            }
            var btnTagBuilder     = new TagBuilder("button");
            var btnHtmlAttributes = options.HtmlAttributes ?? new Dictionary <string, object>();

            if (!btnHtmlAttributes.ContainsKey("type"))
            {
                btnHtmlAttributes.Add("type", "button");
            }
            if (!allowAccess && !btnHtmlAttributes.ContainsKey("disabled"))
            {
                btnHtmlAttributes.Add("disabled", "disabled");
            }
            btnTagBuilder.MergeAttributes(btnHtmlAttributes);
            if (options.UseIco)
            {
                var icoTagBuilder = new TagBuilder("i");
                icoTagBuilder.MergeAttributes(options.IcoHtmlAttributes);
                btnTagBuilder.InnerHtml.AppendHtml(icoTagBuilder);
                btnTagBuilder.InnerHtml.Append(" ");
            }
            btnTagBuilder.InnerHtml.Append(options.Text);
            return(btnTagBuilder);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create auth link
        /// </summary>
        /// <param name="htmlHelper">Html helper</param>
        /// <param name="options">Options</param>
        /// <returns>Return html content</returns>
        public static IHtmlContent AuthLink(this IHtmlHelper htmlHelper, AuthButtonOptions options)
        {
            if (options == null || (options.UseNowVerifyResult && !options.AllowAccess))
            {
                return(HtmlString.Empty);
            }
            if (!options.UseNowVerifyResult)
            {
                var allowAccess = AuthorizationManager.Authorize(new AuthorizeOptions()
                {
                    Action        = options.ActionOptions?.Action,
                    Controller    = options.ActionOptions?.Controller,
                    Area          = options.ActionOptions?.Area,
                    Application   = ApplicationManager.Current,
                    Claims        = HttpContextHelper.Current.User.Claims.ToDictionary(c => c.Type, c => c.Value),
                    ActionContext = htmlHelper?.ViewContext,
                    Method        = options.ActionOptions?.Method
                })?.AllowAccess ?? false;
                if (!allowAccess)
                {
                    return(HtmlString.Empty);
                }
            }
            var btnTagBuilder     = new TagBuilder("a");
            var btnHtmlAttributes = options.HtmlAttributes ?? new Dictionary <string, object>();

            if (!btnHtmlAttributes.ContainsKey("href"))
            {
                btnHtmlAttributes.Add("href", "javascript:void(0)");
            }
            btnTagBuilder.MergeAttributes(btnHtmlAttributes);
            if (options.UseIco)
            {
                var icoTagBuilder = new TagBuilder("i");
                icoTagBuilder.MergeAttributes(options.IcoHtmlAttributes);
                btnTagBuilder.InnerHtml.AppendHtml(icoTagBuilder);
                btnTagBuilder.InnerHtml.Append(" ");
            }
            btnTagBuilder.InnerHtml.Append(options.Text);
            return(btnTagBuilder);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create auth html element
        /// </summary>
        /// <param name="htmlHelper">Html helper</param>
        /// <param name="tagName">Tag name</param>
        /// <param name="options">Options</param>
        /// <returns>Return html content</returns>
        public static IHtmlContent AuthHtmlElement(this IHtmlHelper htmlHelper, string tagName, AuthButtonOptions options)
        {
            if (options == null || (options.UseNowVerifyResult && !options.AllowAccess))
            {
                return(HtmlString.Empty);
            }
            if (!options.UseNowVerifyResult)
            {
                var allowAccess = AuthorizationManager.Authorize(new AuthorizeOptions()
                {
                    Action        = options.ActionOptions?.Action,
                    Controller    = options.ActionOptions?.Controller,
                    Application   = ApplicationManager.Current,
                    Claims        = HttpContextHelper.Current.User.Claims.ToDictionary(c => c.Type, c => c.Value),
                    ActionContext = htmlHelper?.ViewContext,
                    Method        = options.ActionOptions?.Method,
                    Area          = options.ActionOptions?.Area
                })?.AllowAccess ?? false;
                if (!allowAccess)
                {
                    return(HtmlString.Empty);
                }
            }
            var htmlTagBuilder = new TagBuilder(tagName);
            var htmlAttributes = options.HtmlAttributes ?? new Dictionary <string, object>();

            htmlTagBuilder.MergeAttributes(htmlAttributes);
            if (options.UseIco)
            {
                var icoTagBuilder = new TagBuilder("i");
                icoTagBuilder.MergeAttributes(options.IcoHtmlAttributes);
                htmlTagBuilder.InnerHtml.AppendHtml(icoTagBuilder);
                htmlTagBuilder.InnerHtml.Append(" ");
            }
            htmlTagBuilder.InnerHtml.Append(options.Text);
            return(htmlTagBuilder);
        }