Click here to Skip to main content
15,887,585 members
Home / Discussions / ASP.NET
   

ASP.NET

 
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 
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 
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 PinPopular
Richard MacCutchan11-Feb-14 7:09
mveRichard MacCutchan11-Feb-14 7:09 

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.