ASP.NET Core 2.1 Using ActionFilters in View
up vote
-1
down vote
favorite
I have an application that uses Jwt to authenticate my user. Whenever I want to verify if my user has the appropriate role, I use a filter over my controller like this
[TypeFilter(typeof(ValidateRolesFilter), Arguments = new object {
ApplicationGlobals.ApplicationSecretKey, RoleGlobals.SystemAdministrator
})]
public IActionResult Index()
{
return View();
}
However, this only applies to my controller. Is it possible to use this in the view to segment off certain sections based on roles as well? Or is there a better way of doing this?
asp.net asp.net-core asp.net-core-mvc
|
show 9 more comments
up vote
-1
down vote
favorite
I have an application that uses Jwt to authenticate my user. Whenever I want to verify if my user has the appropriate role, I use a filter over my controller like this
[TypeFilter(typeof(ValidateRolesFilter), Arguments = new object {
ApplicationGlobals.ApplicationSecretKey, RoleGlobals.SystemAdministrator
})]
public IActionResult Index()
{
return View();
}
However, this only applies to my controller. Is it possible to use this in the view to segment off certain sections based on roles as well? Or is there a better way of doing this?
asp.net asp.net-core asp.net-core-mvc
Can you apply the filter to an action method, which can be included in your view (Html.action
helper)
– Shyju
Nov 12 at 4:34
Hello! I've edited my question to show how I would use it. How do I use it in my Html.action helper?
– JianYA
Nov 12 at 4:37
Include those sections usingHtml.Action
helper and apply your filters to those action methods ( which returns the HTML markup for those segments).
– Shyju
Nov 12 at 4:38
Sorry is there an example somewhere I can see? I'm having a difficult time picturing what it would look like.
– JianYA
Nov 12 at 4:39
To use authentication/authorization in the view file, you can simply inject theIAuthorizationService
to authorize anything against your custom policy as you like. There's no need to put every thing in your custom filter.
– itminus
Nov 12 at 7:17
|
show 9 more comments
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have an application that uses Jwt to authenticate my user. Whenever I want to verify if my user has the appropriate role, I use a filter over my controller like this
[TypeFilter(typeof(ValidateRolesFilter), Arguments = new object {
ApplicationGlobals.ApplicationSecretKey, RoleGlobals.SystemAdministrator
})]
public IActionResult Index()
{
return View();
}
However, this only applies to my controller. Is it possible to use this in the view to segment off certain sections based on roles as well? Or is there a better way of doing this?
asp.net asp.net-core asp.net-core-mvc
I have an application that uses Jwt to authenticate my user. Whenever I want to verify if my user has the appropriate role, I use a filter over my controller like this
[TypeFilter(typeof(ValidateRolesFilter), Arguments = new object {
ApplicationGlobals.ApplicationSecretKey, RoleGlobals.SystemAdministrator
})]
public IActionResult Index()
{
return View();
}
However, this only applies to my controller. Is it possible to use this in the view to segment off certain sections based on roles as well? Or is there a better way of doing this?
asp.net asp.net-core asp.net-core-mvc
asp.net asp.net-core asp.net-core-mvc
edited Nov 12 at 4:37
Tetsuya Yamamoto
14k41939
14k41939
asked Nov 12 at 4:33
JianYA
432924
432924
Can you apply the filter to an action method, which can be included in your view (Html.action
helper)
– Shyju
Nov 12 at 4:34
Hello! I've edited my question to show how I would use it. How do I use it in my Html.action helper?
– JianYA
Nov 12 at 4:37
Include those sections usingHtml.Action
helper and apply your filters to those action methods ( which returns the HTML markup for those segments).
– Shyju
Nov 12 at 4:38
Sorry is there an example somewhere I can see? I'm having a difficult time picturing what it would look like.
– JianYA
Nov 12 at 4:39
To use authentication/authorization in the view file, you can simply inject theIAuthorizationService
to authorize anything against your custom policy as you like. There's no need to put every thing in your custom filter.
– itminus
Nov 12 at 7:17
|
show 9 more comments
Can you apply the filter to an action method, which can be included in your view (Html.action
helper)
– Shyju
Nov 12 at 4:34
Hello! I've edited my question to show how I would use it. How do I use it in my Html.action helper?
– JianYA
Nov 12 at 4:37
Include those sections usingHtml.Action
helper and apply your filters to those action methods ( which returns the HTML markup for those segments).
– Shyju
Nov 12 at 4:38
Sorry is there an example somewhere I can see? I'm having a difficult time picturing what it would look like.
– JianYA
Nov 12 at 4:39
To use authentication/authorization in the view file, you can simply inject theIAuthorizationService
to authorize anything against your custom policy as you like. There's no need to put every thing in your custom filter.
– itminus
Nov 12 at 7:17
Can you apply the filter to an action method, which can be included in your view (
Html.action
helper)– Shyju
Nov 12 at 4:34
Can you apply the filter to an action method, which can be included in your view (
Html.action
helper)– Shyju
Nov 12 at 4:34
Hello! I've edited my question to show how I would use it. How do I use it in my Html.action helper?
– JianYA
Nov 12 at 4:37
Hello! I've edited my question to show how I would use it. How do I use it in my Html.action helper?
– JianYA
Nov 12 at 4:37
Include those sections using
Html.Action
helper and apply your filters to those action methods ( which returns the HTML markup for those segments).– Shyju
Nov 12 at 4:38
Include those sections using
Html.Action
helper and apply your filters to those action methods ( which returns the HTML markup for those segments).– Shyju
Nov 12 at 4:38
Sorry is there an example somewhere I can see? I'm having a difficult time picturing what it would look like.
– JianYA
Nov 12 at 4:39
Sorry is there an example somewhere I can see? I'm having a difficult time picturing what it would look like.
– JianYA
Nov 12 at 4:39
To use authentication/authorization in the view file, you can simply inject the
IAuthorizationService
to authorize anything against your custom policy as you like. There's no need to put every thing in your custom filter.– itminus
Nov 12 at 7:17
To use authentication/authorization in the view file, you can simply inject the
IAuthorizationService
to authorize anything against your custom policy as you like. There's no need to put every thing in your custom filter.– itminus
Nov 12 at 7:17
|
show 9 more comments
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256009%2fasp-net-core-2-1-using-actionfilters-in-view%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256009%2fasp-net-core-2-1-using-actionfilters-in-view%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you apply the filter to an action method, which can be included in your view (
Html.action
helper)– Shyju
Nov 12 at 4:34
Hello! I've edited my question to show how I would use it. How do I use it in my Html.action helper?
– JianYA
Nov 12 at 4:37
Include those sections using
Html.Action
helper and apply your filters to those action methods ( which returns the HTML markup for those segments).– Shyju
Nov 12 at 4:38
Sorry is there an example somewhere I can see? I'm having a difficult time picturing what it would look like.
– JianYA
Nov 12 at 4:39
To use authentication/authorization in the view file, you can simply inject the
IAuthorizationService
to authorize anything against your custom policy as you like. There's no need to put every thing in your custom filter.– itminus
Nov 12 at 7:17