csharp basicAuth.cs

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp basicAuth.cs相关的知识,希望对你有一定的参考价值。

//Add Security Class  - (this is will be used by basicauthentication class to authenticate users)
//Add a BasicAuthenticationAttribute Class
//Add BasicAuthentication FIlter Attribute to controller/Method - [BasicAuthentication]
//Calling From MVC- client.DefaultRequestHeaders.Add("Authorization", "Basic " + base64string);

==========================================================================================================================================
//Security Class

public static class EmployeeSecurity
    {
        public static bool Login(string username , string password)
        {
            using (var db = new TestDbContext())
            {
                return db.Users.Any(a => a.Username.Equals(username,StringComparison.OrdinalIgnoreCase)
                && a.Password.Equals(password, StringComparison.OrdinalIgnoreCase));
            }
        }
    }

	
==========================================================================================================================================
BasicAuthenticationAttribute Class

public class BasicAuthenticationAttribute : AuthorizationFilterAttribute
    {
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            if (actionContext.Request.Headers.Authorization == null)
            {
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
            }
            else
            {
                var authorizationToken = actionContext.Request.Headers.Authorization.Parameter;
                var decodedAuthorizationToken = Encoding.UTF8.GetString(
                                                Convert.FromBase64String(authorizationToken));

                var username = decodedAuthorizationToken.Split(':')[0];
                var password = decodedAuthorizationToken.Split(':')[1];

                if (EmployeeSecurity.Login(username,password))
                {
                    Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(username),null );
                }
                else
                {
                    actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
                }
            }
        }
    }
	

=====================================================================================================================================
Controller

		[BasicAuthentication]
        public IHttpActionResult Get()
        {
            var username = Thread.CurrentPrincipal.Identity.Name;

            if (username.Equals("victor"))
            {
                var maleEmployees = db.Employees.Where(a => a.Gender == "Male").ToList();
                return Ok(maleEmployees);
            }
            else if (username.Equals("chidi"))
            {
                var femaleEmployees = db.Employees.Where(a => a.Gender == "Female").ToList();
                return Ok(femaleEmployees);
            }

            return BadRequest("invalid data");
            
        }

以上是关于csharp basicAuth.cs的主要内容,如果未能解决你的问题,请参考以下文章

csharp 例如-CSHARP-GroupDocs.Conversion.Examples.CSharp - 程序 - ConvertFilesToDifferentFormats.cs

csharp 例如-CSHARP-GroupDocs.Conversion.Examples.CSharp渲染,RenderPSDImageAsPath.cs

csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs

csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs

csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs

csharp 例如-CSHARP-GroupDocs.Search.Examples.CSharp索引,AddDocumentToIndex.cs