Exemple #1
0
        public override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request.Headers.Authorization == null)
            {
                return(request.CreateResponse(HttpStatusCode.Unauthorized, "Unauthorized"));
            }
            else
            {
                string token = request.Headers.GetValues("Authorization").FirstOrDefault();
                if (token != null)
                {
                    byte[]   data          = Convert.FromBase64String(token);
                    string   decodedString = Encoding.UTF8.GetString(data);
                    string[] tokensValues  = decodedString.Split(':');

                    UserMaser ObjUser = new CredentialChecker().CheckCredential(tokensValues[0], tokensValues[1]);
                    if (ObjUser != null)
                    {
                        IPrincipal principal = new GenericPrincipal(new GenericIdentity(ObjUser.name), ObjUser.UserRole.Split(','));
                        Thread.CurrentPrincipal  = principal;
                        HttpContext.Current.User = principal;
                    }
                    else
                    {
                        //The user is unauthorize and return 401 status
                        var response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
                        var tsc      = new TaskCompletionSource <HttpResponseMessage>();
                        tsc.SetResult(response);
                        return(tsc.Task);
                    }
                }
            }
        }
Exemple #2
0
        public async Task Invoke(HttpContext context)
        {
            try
            {
                var request = context.Request;
                var tokens  = request.Headers["Authorization"].FirstOrDefault();
                if (tokens != null)
                {
                    byte[]   data          = Convert.FromBase64String(tokens);
                    string   decodedString = Encoding.UTF8.GetString(data);
                    string[] tokensValues  = decodedString.Split(':');

                    UserModel ObjUser = new CredentialChecker().CheckCredential(tokensValues[0], tokensValues[1]);
                    if (ObjUser != null)
                    {
                        IPrincipal principal = new GenericPrincipal(new GenericIdentity(ObjUser.Name), ObjUser.UserRoles.Split(','));
                        Thread.CurrentPrincipal = principal;
                        //HttpContext.Current.User = principal;
                    }
                    else
                    {
                        //The user is unauthorize and return 401 status
                        var response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
                        var tsc      = new TaskCompletionSource <HttpResponseMessage>();
                        tsc.SetResult(response);
                        await tsc.Task;
                    }
                }
                else
                {
                    //Bad Request request because Authentication header is set but value is null
                    var response = new HttpResponseMessage(HttpStatusCode.Forbidden);
                    var tsc      = new TaskCompletionSource <HttpResponseMessage>();
                    tsc.SetResult(response);
                    await tsc.Task;
                }
            }
            catch
            {
                //User did not set Authentication header
                var response = new HttpResponseMessage(HttpStatusCode.Forbidden);
                var tsc      = new TaskCompletionSource <HttpResponseMessage>();
                tsc.SetResult(response);
                await tsc.Task;
            }
        }