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
Hrides ThakurPosted May 7, 2019, 3:41 AM