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
Hrides Thakur
NA
292
49.7k
How to add Jwt token authentication in Web API
May 7 2019 3:29 AM
I have created web api project by selecting an empty project in vs.
I want to use jwt token-based authentication, I have installed nugate package IdentityModel.Tokens.Jwt.
I have added Authentication filter (created a custom class AuthenticatAtribue and implement iAuthenticationFilter)
please provide code for below methods
public
async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
// 1. Look for credentials in the request.
HttpRequestMessage request = context.Request;
AuthenticationHeaderValue authorization = request.Headers.Authorization;
// 2. If there are no credentials, do nothing.
if
(authorization ==
null
)
{
return
;
}
// 3. If there are credentials but the filter does not recognize the
// authentication scheme, do nothing.
if
(authorization.Scheme !=
"Basic"
)
// is basic ok here i want token based authentication
{
return
;
}
// 4. If there are credentials that the filter understands, try to validate them.
// 5. If the credentials are bad, set the error result.
if
(String.IsNullOrEmpty(authorization.Parameter))
{
context.ErrorResult =
"what should write here"
return
;
}
Tuple<
string
,
string
> userNameAndPassword =
null
;
// ExtractUserNameAndPassword(authorization.Parameter);
if
(userNameAndPassword ==
null
)
{
//context.ErrorResult = new AuthenticationFailureResult("Invalid credentials", request);
context.ActionContext.Response =
new
HttpResponseMessage(HttpStatusCode.Unauthorized);
}
// string userName = userNameAndPassword.Item1;
// string password = userNameAndPassword.Item2;
var identity =
new
GenericIdentity(); what should i write here
IPrincipal principal=
new
GenericPrincipal(identity,
null
);
//// await AuthenticateAsync(userName, password, cancellationToken);
if
(principal ==
null
)
{
context.ErrorResult =
null
;
// new AuthenticationFailureResult("Invalid username or password", request);
}
// 6. If the credentials are valid, set principal.
else
{
context.Principal = principal;
}
}
public
Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
{
please provide code for this section
Reply
Answers (
1
)
Insert Html Table values in database using ASP.Net MVC
pdf view in mvc plz give functionality