Click here to Skip to main content
15,886,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I am writing to seek help, in how do I construct a method/or class that will allow me to login using the details from the db instead of written in code.

C#
namespace API
{
    public class DummyPrincipalProvider : IProvidePrincipal
    {
        private const string Username = "###";
        private const string Password = "####";

        public IPrincipal CreatePrincipal(string username, string password)
        {
            if (username != Username || password != Password)
            {
                return null;
                //Page.ClientScript.RegisterStartupScript(GetType(), "done", "alert('your message');", true);
            }

            var identity = new GenericIdentity(Username);

            IPrincipal principal = new GenericPrincipal(identity, new[] { "trial" });
            return principal;
        } 
    }
}

C#
namespace API
{
    using System;
    using System.Collections.Generic;
    
    public partial class api_login
    {
        public string ID { get; set; }
        public string username { get; set; }
        public string password { get; set; }
        public string description { get; set; }
        public string role { get; set; }
    }
}


Updated code
C#
namespace API
{
    public class BasicAuthMessageHandler : DelegatingHandler
    {
        private const string BasicAuthResponseHeader = "WWW-Authenticate";
        private const string BasicAuthResponseHeaderValue = "Basic";

        public IProvidePrincipal PrincipalProvider { 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],
                       };
        }
    }
}


Any help would be very much appreciated.
Many thanks for your time and help.
Posted
Updated 9-Jan-14 22:56pm
v3
Comments
ZurdoDev 9-Jan-14 7:49am    
Do you know anything about ADO.Net or SqlCommand and SqlConnection classes?
miss786 9-Jan-14 8:25am    
I am aware of SqlCommand and SqlConnection classes a little a bit. Are you suggesting, that I create a sql class and reference it the above DummyPrincipalProvider class. Thank you so much or reply.
ZurdoDev 9-Jan-14 8:33am    
Sure. With those classes you can call Sql and do whatever you need to.
miss786 9-Jan-14 11:30am    
Hi, Thank you for your suggestion. i am started to implement the sql class but I am also 'basic authenticate' class which authorizes web api feed and was wondering how would i integrate both classes? Thank you
Rajesh_DotNet 9-Jan-14 8:46am    
Have tried using ASPMembership login control? asp.net has provided login control which, you may need not to write your manual code for login. see this link http://msdn.microsoft.com/en-us/library/ms178329.aspx, it will be surely helpful and a standard method to be used. Also it is suggested by msdn... :)

1 solution

Take a look at an example here[+]
 
Share this answer
 
Comments
miss786 10-Jan-14 4:54am    
Thank you for your response and example. I have updated my original post, with the BasicAuthMessageHandler class and api-login class. I would like to ask, if i could create user verification method in the BasicAuthMessageHandler class. Thank you for your time and help.
CBadger 10-Jan-14 8:05am    
What are you trying to do with this code? :-)

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],
};
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900