TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Naruto Reddy
NA
123
38.6k
Middlaware authentication for Web Api allowanonymous method
Jul 26 2020 8:36 AM
I have a middle ware class as below and expecting token for allow-anonymous method. Can some suggest me what am i missing over here.
Middleware class as follows :
namespace
filetrs
{
public
class
TestingAuthenticationFilter : IAuthenticationFilter
{
public
async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
var alias = GetAliasFromJwt(context.Request.Headers.Authorization.ToString());
await Task.Yield();
}
public
Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
{
return
Task.CompletedTask;
}
public
static
string
GetAliasFromJwt(
string
jwt)
{
string
alias, extractedToken;
alias = extractedToken =
string
.Empty;
string
tokenTypeToRemove =
"Bearer "
;
if
(tokenTypeToRemove.Length > 0)
{
extractedToken = jwt.Substring(tokenTypeToRemove.Length);
var handler =
new
JwtSecurityTokenHandler();
var jwtSecurityToken = handler.ReadToken(extractedToken)
as
JwtSecurityToken;
alias = jwtSecurityToken.Claims.First(claim => claim.Type ==
"abc"
).Value.Split(
'@'
).First();
}
return
alias;
}
}
Please find APi class as well
public
static
class
WebApiConfig
{
public
static
void
Register(HttpConfiguration config)
{
//var cors = new EnableCorsAttribute("*", "*", "*");
//config.EnableCors(cors);
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name:
"DefaultApi"
,
routeTemplate:
"api/{controller}/{id}"
,
defaults:
new
{ id = RouteParameter.Optional } );
config.Filters.Add(
new
TestingAuthenticationFilter());
config.Filters.Add(
new
ExceptionHandlingAttribute());
}}
ConfigAuth method from startup.cs as follows
public
void
ConfigureAuth(IAppBuilder app)
{
app.UseWindowsAzureActiveDirectoryBearerAuthentication
(
new
WindowsAzureActiveDirectoryBearerAuthenticationOptions
{ Tenant = tenant, TokenValidationParameters =
new
TokenValidationParameters { ValidAudience = clientId }
});
I am unable to find the solution. Please someone help me on this?
Reply
Answers (
2
)
difference between Abstarct and interface in c# 8.0
security issue to Send mail using .Net (System.net.mail)