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

ASP.NET

 
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 
Hi,

I am trying to get the following code read credentials from the user class but I experiencing the following principal error -- "The name 'PrincipalProvider' does not exist in the current context".

C#
public class BasicAuthMessageHandler : DelegatingHandler
    {
        bool _RequireSsl = true;
        public bool RequireSsl
        {
            get { return _RequireSsl; }
            set { _RequireSsl = value; }
        }
        
        private const string BasicAuthResponseHeader = "WWW-Authenticate";
        private const string BasicAuthResponseHeaderValue = "Basic";


        [Inject]
        public iUser Repository { get; set; }


        protected override System.Threading.Tasks.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)
                {
                    Thread.CurrentPrincipal = PrincipalProvider
                        .CreatePrincipal(parsedCredentials.username, parsedCredentials.password);
                }
            }
            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 Authenticate(HttpContextBase context)
        {
            if (_RequireSsl && !context.Request.IsSecureConnection && !context.Request.IsLocal) return false;

            if (!context.Request.Headers.AllKeys.Contains("Authorization")) return false;

            string authHeader = context.Request.Headers["Authorization"];

            IPrincipal principal;
            if (TryGetPrincipal(authHeader, out principal))
            {
                HttpContext.Current.User = principal;
                return true;
            }
            return false;
        }

        private bool TryGetPrincipal(string authHeader, out IPrincipal principal)
        {
            var creds = ParseAuthHeader(authHeader);
            if (creds != null)
            {
                if (TryGetPrincipal(creds[0], creds[1], out principal)) return true;
            }

            principal = null;
            return false;
        }

        private string[] ParseAuthHeader(string authHeader)
        {
            // Check this is a Basic Auth header 
            if (authHeader == null || authHeader.Length == 0 || !authHeader.StartsWith("Basic")) return null;

            // Pull out the Credentials with are seperated by ':' and Base64 encoded 
            string base64Credentials = authHeader.Substring(6);
            string[] credentials = Encoding.ASCII.GetString(Convert.FromBase64String(base64Credentials)).Split(new char[] { ':' });

            if (credentials.Length != 2 || string.IsNullOrEmpty(credentials[0]) || string.IsNullOrEmpty(credentials[0])) return null;

            // Okay this is the credentials
            return credentials;
        }

        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;
            }
            else
            {
                principal = null;
                return false;
            }
        }


User class methods:

public api_login GetData(string userName)
{
return db.api_login.FirstOrDefault(c => c.username == userName);
}


public api_login Validate2(string userName, string Password)
{

return db.api_login.FirstOrDefault(u => u.username == userName && u.password == Password);
}

Many thans for your help and time.
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 
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 

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.