Click here to Skip to main content
15,914,386 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: How to insert a .swf file in a ASP.Net page Pin
thatraja13-Feb-14 20:28
professionalthatraja13-Feb-14 20:28 
GeneralRe: How to insert a .swf file in a ASP.Net page Pin
Kandepu Rajesh13-Feb-14 20:50
Kandepu Rajesh13-Feb-14 20:50 
GeneralRe: How to insert a .swf file in a ASP.Net page Pin
thatraja13-Feb-14 20:55
professionalthatraja13-Feb-14 20:55 
Questionweb api - basic authentication (principal error) Pin
miss78613-Feb-14 0:06
miss78613-Feb-14 0:06 
AnswerRe: web api - basic authentication (principal error) Pin
Richard Deeming13-Feb-14 1:20
mveRichard Deeming13-Feb-14 1:20 
GeneralRe: web api - basic authentication (principal error) Pin
miss78614-Feb-14 2:07
miss78614-Feb-14 2:07 
GeneralRe: web api - basic authentication (principal error) Pin
Richard Deeming14-Feb-14 3:00
mveRichard Deeming14-Feb-14 3:00 
GeneralRe: web api - basic authentication (principal error) Pin
miss78627-Feb-14 4:27
miss78627-Feb-14 4:27 
Hi Richard,

Thank you so much for help with prior issue with the basic authentication. I am trying to add error messages/exceptions, if the user has either failed to Login or if user credentials are blank. In the code below, I am using throw new HttpResponseException(HttpStatusCode.Unauthorized); , and this currently not showing up for failing user log-ins. I am testing this on the built-in 'values' api controller class by using the '[authorize]' function on top of the values api controller class.
C#
private bool TryGetPrincipal(string userName, string password, out IPrincipal principal)
{

    // data access in a repository or separate layer/library.
    api_login user = repository.Validate2(userName, password);

   // int failedAttempts = 0;
    if (user.username != null)
    {
        // once the user is verified, assign it to an IPrincipal with the identity name and applicable roles
        //failedAttempts += 1;
        principal = new GenericPrincipal(new GenericIdentity(user.username), null);
    }

    else if (user.username == null)
    {
        //throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, String.Format("this identity does not exist")));

        throw new HttpResponseException(HttpStatusCode.Unauthorized);

    }
    principal = null;

    return false;
}


This is what my BasicAuthHandler class currently looks like. I am not sure whether i would need to create and I am currently not getting anything on the client-side if user credentials are null or pressed cancel on the login dialog.
C#
public class BasicAuthHandler : DelegatingHandler
    {

        private const string BasicAuthResponseHeader = "WWW-Authenticate";
        private const string BasicAuthResponseHeaderValue = "Basic";

        //private readonly iUser repository;

        public BasicAuthHandler(iUser repository)
        {
            this.repository = repository;
        }

        [Inject]
        iUser repository { get; set; }


        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            AuthenticationHeaderValue authValue = request.Headers.Authorization;


            if (authValue == null || authValue.Scheme != BasicAuthResponseHeaderValue)
            {
                return Unauthorized(request);
            }
            string[] credentials = Encoding.ASCII.GetString(Convert.FromBase64String(authValue.Parameter)).Split(new[] { ':' });
            if (credentials.Length != 2 || string.IsNullOrEmpty(credentials[0]) || string.IsNullOrEmpty(credentials[1]))
            {
                return Unauthorized(request);
           
            }
            api_login user = repository.Validate2(credentials[0], credentials[1]);
            if (user == null)
            {
                return Unauthorized(request);
            }
            IPrincipal principal = new GenericPrincipal(new GenericIdentity(user.username, BasicAuthResponseHeaderValue), null);
            Thread.CurrentPrincipal = principal;
            HttpContext.Current.User = principal;

            return base.SendAsync(request, cancellationToken);
        }

        private Task<HttpResponseMessage> Unauthorized(HttpRequestMessage request)
        {
            var response = request.CreateResponse(HttpStatusCode.Unauthorized);
            response.Headers.Add(BasicAuthResponseHeader, BasicAuthResponseHeaderValue);
            var task = new TaskCompletionSource<HttpResponseMessage>();
            task.SetResult(response);
            return task.Task;
        }

        private api_login ParseAuthorizationHeader(string authHeader)
        {
            string[] credentials = Encoding.ASCII.GetString(Convert.FromBase64String(authHeader)).Split(new[] { ':' });
            if (credentials.Length != 2 || string.IsNullOrEmpty(credentials[0]) || string.IsNullOrEmpty(credentials[1])) return null;

            return new api_login()
            {
                username = credentials[0],
                password = credentials[1],
            };
        }

        private bool TryGetPrincipal(string userName, string password, out IPrincipal principal)
        {

            // data access in a repository or separate layer/library.
            api_login user = repository.Validate2(userName, password);

           // int failedAttempts = 0;
            if (user.username != null)
            {
                // once the user is verified, assign it to an IPrincipal with the identity name and applicable roles
                //failedAttempts += 1;
                principal = new GenericPrincipal(new GenericIdentity(user.username), null);
            }

            else if (user.username == null)
            {
                //throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, String.Format("this identity does not exist")));
                
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
               
            }
            principal = null;

            return false;
        }
           
    }

Many thanks for your time and help.
GeneralRe: web api - basic authentication (principal error) Pin
Richard Deeming27-Feb-14 4:38
mveRichard Deeming27-Feb-14 4:38 
GeneralRe: web api - basic authentication (principal error) Pin
miss78627-Feb-14 6:31
miss78627-Feb-14 6:31 
QuestionThis Control is invalid -Error Pin
Member 799271612-Feb-14 21:43
Member 799271612-Feb-14 21:43 
AnswerRe: This Control is invalid -Error Pin
Richard Deeming13-Feb-14 0:59
mveRichard Deeming13-Feb-14 0:59 
QuestionPlz help for button data insert in to table... Pin
Patel Vinay V12-Feb-14 16:23
Patel Vinay V12-Feb-14 16:23 
AnswerRe: Plz help for button data insert in to table... Pin
Ahmed Bensaid12-Feb-14 22:31
professionalAhmed Bensaid12-Feb-14 22:31 
AnswerRe: Plz help for button data insert in to table... Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)13-Feb-14 8:07
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)13-Feb-14 8:07 
QuestionASPNET AJAX sys.webserver configuration for NET 4+ Pin
Kishore Goduguluri12-Feb-14 4:21
Kishore Goduguluri12-Feb-14 4:21 
QuestionTime Table Pin
Prakaz2511-Feb-14 23:37
professionalPrakaz2511-Feb-14 23:37 
AnswerRe: Time Table Pin
Wonderful Coder12-Feb-14 0:11
Wonderful Coder12-Feb-14 0:11 
SuggestionRe: Time Table Pin
Kornfeld Eliyahu Peter12-Feb-14 0:11
professionalKornfeld Eliyahu Peter12-Feb-14 0:11 
Questionweb api c# - error Pin
miss78611-Feb-14 5:56
miss78611-Feb-14 5:56 
AnswerRe: web api c# - error Pin
Kornfeld Eliyahu Peter11-Feb-14 7:01
professionalKornfeld Eliyahu Peter11-Feb-14 7:01 
QuestionRe: web api c# - error Pin
Richard MacCutchan11-Feb-14 7:09
mveRichard MacCutchan11-Feb-14 7:09 
Question[CLOSED] Aes Encryption fails Pin
vishalgpt11-Feb-14 4:57
vishalgpt11-Feb-14 4:57 
AnswerRe: Aes Encryption fails Pin
Richard Deeming11-Feb-14 8:27
mveRichard Deeming11-Feb-14 8:27 
GeneralRe: Aes Encryption fails Pin
vishalgpt11-Feb-14 16:21
vishalgpt11-Feb-14 16:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.