Click here to Skip to main content
15,908,173 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: Click Event in Menu Control not firing Pin
Rob Achmann19-Feb-14 3:30
Rob Achmann19-Feb-14 3:30 
GeneralRe: Click Event in Menu Control not firing Pin
ZurdoDev19-Feb-14 3:51
professionalZurdoDev19-Feb-14 3:51 
AnswerRe: Click Event in Menu Control not firing Pin
Prasad Vj16-Feb-14 21:09
professionalPrasad Vj16-Feb-14 21:09 
Questionmvc 4 Ajax.BeginForm with Web API? Pin
Rob Achmann15-Feb-14 15:06
Rob Achmann15-Feb-14 15:06 
AnswerRe: mvc 4 Ajax.BeginForm with Web API? Pin
Rob Achmann16-Feb-14 10:48
Rob Achmann16-Feb-14 10:48 
AnswerRe: mvc 4 Ajax.BeginForm with Web API? Pin
Rob Achmann19-Feb-14 3:33
Rob Achmann19-Feb-14 3:33 
QuestionBuild API in ASP.NET Web Form Pin
Hy Chanhan15-Feb-14 5:14
professionalHy Chanhan15-Feb-14 5:14 
AnswerRe: Build API in ASP.NET Web Form Pin
Rob Achmann16-Feb-14 1:51
Rob Achmann16-Feb-14 1:51 
AnswerRe: Build API in ASP.NET Web Form Pin
ZurdoDev18-Feb-14 15:26
professionalZurdoDev18-Feb-14 15:26 
Questionsource code Pin
punith Kumar Raj15-Feb-14 0:28
punith Kumar Raj15-Feb-14 0:28 
AnswerRe: source code Pin
Tom Marvolo Riddle15-Feb-14 0:43
professionalTom Marvolo Riddle15-Feb-14 0:43 
AnswerRe: source code Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)15-Feb-14 1:43
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)15-Feb-14 1:43 
QuestionI am getting RSAProtection Error, please help me Pin
indian14314-Feb-14 7:16
indian14314-Feb-14 7:16 
QuestionSelect multiple row of gridview using Shift+select functionality without using checkboxes. Pin
maheshakamudli14-Feb-14 3:12
maheshakamudli14-Feb-14 3:12 
AnswerRe: Select multiple row of gridview using Shift+select functionality without using checkboxes. Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)14-Feb-14 19:13
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)14-Feb-14 19:13 
QuestionAbout gate way Pin
punith Kumar Raj14-Feb-14 2:39
punith Kumar Raj14-Feb-14 2:39 
AnswerRe: About gate way Pin
Richard Andrew x6414-Feb-14 19:15
professionalRichard Andrew x6414-Feb-14 19:15 
AnswerRe: About gate way Pin
Tom Marvolo Riddle14-Feb-14 19:23
professionalTom Marvolo Riddle14-Feb-14 19:23 
QuestionHow to insert a .swf file in a ASP.Net page Pin
Kandepu Rajesh13-Feb-14 19:04
Kandepu Rajesh13-Feb-14 19:04 
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 
It looks like some of that code has come from this blog post[^], but you're missing the PrincipalProvider property, the IProvidePrincipal interface, and the implementation of that interface.

You've got quite a few methods which don't appear to be called. Your RequireSsl property is only used from the Authenticate method, which is never called.

Looking at the Validate2 method, it appears you're storing passwords in plain text, which is a very bad idea. You should be storing salted hashes of the password:
Salted Password Hashing - Doing it Right[^]

Removing the unused methods and property, it looks like you need to use your TryGetPrincipal method instead of PrincipalProvider.CreatePrincipal to get the IPrincipal for the request:
C#
public class BasicAuthMessageHandler : DelegatingHandler
{
    private const string BasicAuthResponseHeader = "WWW-Authenticate";
    private const string BasicAuthResponseHeaderValue = "Basic";

    [Inject]
    public iUser Repository { get; set; }
    
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        AuthenticationHeaderValue authValue = request.Headers.Authorization;
        if (authValue != null && !String.IsNullOrWhiteSpace(authValue.Parameter))
        {
            api_login parsedCredentials = ParseAuthorizationHeader(authValue.Parameter);
            if (parsedCredentials != null)
            {
                IPrincipal principal;
                if (TryGetPrincipal(parsedCredentials.username, parsedCredentials.password, out principal))
                {
                    Thread.CurrentPrincipal = principal;
                }
            }
        }
        
        return base.SendAsync(request, cancellationToken).ContinueWith(task =>
        {
            var response = task.Result;
            if (response.StatusCode == HttpStatusCode.Unauthorized && !response.Headers.Contains(BasicAuthResponseHeader))
            {
                response.Headers.Add(BasicAuthResponseHeader, BasicAuthResponseHeaderValue);
            }
            
            return response;
        });
    }
    
    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)
    {
        // this is the method that authenticates against my repository (in this case, hard coded)
        // you can replace this with whatever logic you'd use, but proper separation would put the
        // data access in a repository or separate layer/library.
        api_login user = Repository.Validate2(userName, password);
        
        if (user != null)
        {
            // once the user is verified, assign it to an IPrincipal with the identity name and applicable roles
            principal = new GenericPrincipal(new GenericIdentity(user.username));
            return true;
        }
        
        principal = null;
        return false;
    } 
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: web api - basic authentication (principal error) Pin
miss78614-Feb-14 2:07
miss78614-Feb-14 2:07 

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.