custom-attributes

How can I develop a custom AuthorizeAttribute that accepts a login OR a token?

匆匆过客 提交于 2019-12-10 10:56:21
问题 In my MVC 5 application, I decorate my controllers as follows: [Authorize] public class Controller { .. However, one requirement I have is to use a token to authorize an action without going to the login screen. ie: http://{website}/Action?token={/* token for this user */} Thus, how can I develop a custom AuthorizeAttribute that accepts a login (default behavior) OR a token (custom, required behavior)? In other words, if I use http://{website}/Action , I would be redirected to the login

Remove a route with IOperationFilter in SwashBuckle

☆樱花仙子☆ 提交于 2019-12-10 02:36:57
问题 I am looking for a way to show/hide WebAPI routes in the Swagger documentation using SwashBuckle in a configurable way. Adding [ApiExplorerSettings(IgnoreApi = true)] will indeed hide the routes but I'd need to recompile every time I want that to change. I have looked into creating an IOperationFilter to work with a custom Attribute that I defined. That way I can decorate the routes with a [SwaggerTag("MobileOnly")] and check the web.config or something to see if the route should be shown.

Custom attribute with dash in name using EditorFor/TextBoxFor/TextBox helpers

孤街醉人 提交于 2019-12-10 00:50:18
问题 I am using Knockout-JS to bind properties in my view to my view model. Knockout-JS uses a custom attribute called 'data-bind' that you have to append to controls in which you want to be bound to view model objects. Example: <input type='text' name='first-name' data-bind='value: firstName'/> Notice the 'data-bind' attribute. In my view rendering, I am having trouble rendering a textbox that has this attribute. I am aware the Html.EditorFor, Html.TextBoxFor, and Html.TextBox helpers all take an

AllowMultiple does not work with Property Attributes?

孤人 提交于 2019-12-09 16:51:29
问题 I'm tying to collect all Custom Attributes placed over a Property. There are more than one Attributes of the same type assigned to the Property, but when collecting them , the resulting collection only contains the first Attribute of the specific type: The Attribute class [AttributeUsage(System.AttributeTargets.Property, AllowMultiple = true)] public class ConditionAttribute : Attribute{...} Usage: [ConditionAttribute("Test1")] [ConditionAttribute("Test2")] [ConditionAttribute("Test3")]

MVC ActionFilter like attributes for WCF

南笙酒味 提交于 2019-12-09 16:23:44
问题 Is there a way I can create custom method attributes for WCF that allow me to easily decorate a service method with a pre filter much like MVC uses action filters. I plan to use them for authentication and authorization. This is for a RESTful service whose requests are carrying an authentication cookie. I am more interested in how to create the attributes rather than the authentication side of things. Does the HTTP Toolkit offer anything? Thanks 回答1: WCF has great extensibility - in many ways

default login url on HttpUnauthorizedResult in asp.net mvc

佐手、 提交于 2019-12-09 16:20:12
问题 I have written a custom AuthorizeAttribute which has the following condition in asp.net mvc3 application: public override void OnAuthorization(AuthorizationContext filterContext) { //auth failed, redirect to Sign In if (!filterContext.HttpContext.User.Identity.IsAuthenticated) { filterContext.Result = new HttpUnauthorizedResult(); } } And in my web.config, i have: <authentication mode="Forms"> <forms loginUrl="~/User/SignIn" timeout="2880" /> </authentication> On authentication fail, it

ASP.NET MVC/C#: Can I create valid custom HTML attributes using Html.ActionLink()?

那年仲夏 提交于 2019-12-08 16:05:06
问题 I have the need to put a custom attribute on an anchor which I am constructing using Html.ActionLink() <%: Html.ActionLink("Delete", "Delete", new { id = Model.ID }, new { data-icon = "ui-icon-trash" })%> Using the proper "data-" prefix, as per http://www.w3.org/TR/html5/elements.html#attr-data, I get the following error from Visual Studio. Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access. Since I can't

C# Strongly Typed Attribute member to describe property

故事扮演 提交于 2019-12-08 03:34:50
问题 I was wondering if it was possible to declare An Attribute property that describes a Property so strong typing is required and ideally that intellisense could used to select the property. Class Types works nicely by declaring the member as Type Type But how to get property as a parameter so that "PropName" is not quoted and is strongly typed? So far: The Attibute Class and sample usage looks like [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class MyMeta :

ASP.NET Core Filters - ignore for defined methods

时光怂恿深爱的人放手 提交于 2019-12-07 16:49:44
问题 We implement a log information to our database. I will use a Filters (IActionFilter) functionality for it. I wrote the following class: public class ActionFilter: Attribute, IActionFilter { DateTime start; public void OnActionExecuting(ActionExecutingContext context) { start = DateTime.Now; } public void OnActionExecuted(ActionExecutedContext context) { DateTime end = DateTime.Now; double processTime = end.Subtract(start).TotalMilliseconds; ... some log actions } } Then I have added the

.net Attributes that handle exceptions - usage on a property accessor

时间秒杀一切 提交于 2019-12-07 13:53:24
问题 well I know from my asp.net mvc experience that you can have attributes that handle exceptions (HandleErrorAttribute). As far as I can tell the Controller class has some OnException event which may be integral to this behaviour. However, I want to do something similar in my own code: dream example: public String MyProperty { [ExceptionBehaviour(typeof(FormatException), MyExEnum.ClearValue)] set { _thing.prop = Convert.ToThing(value); } } .... The code above obviously makes very little sense,