public static ApiAuth.Result IsApiAuth(IAuthenticableController c, string validRole = null, IEnumerable <ApiCall.CallParameter> parameters = null, [CallerMemberName] string method = "") { if (string.IsNullOrEmpty(validRole)) { return(IsApiAuth(c, new string[] { }, parameters, method)); } else { return(IsApiAuth(c, validRole.Split(','), parameters, method)); } }
public static ApiAuth.Result IsApiAuth(IAuthenticableController c, string[] validRoles, IEnumerable <ApiCall.CallParameter> parameters = null, [CallerMemberName] string method = "") { var usrmgr = HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>(); string login = null; if (IsApiAuthHeader(c.AuthToken, out login)) { Microsoft.AspNet.Identity.EntityFramework.IdentityUser user = usrmgr.FindByEmail(login); if (user == null) { return(ApiAuth.Result.Invalid(new ApiCall() { IP = c.HostIpAddress, UserId = null, User = null, Id = method, Method = method, Parameters = parameters })); } else { string[] userroles = usrmgr.GetRoles(user.Id).ToArray(); if (validRoles == null) { return(ApiAuth.Result.Valid(new ApiCall() { IP = c.HostIpAddress, UserId = user.Id, User = user.Email, Id = method, Method = method, Parameters = parameters, UserRoles = userroles })); } else if (validRoles.Count() == 0) { return(ApiAuth.Result.Valid(new ApiCall() { IP = c.HostIpAddress, UserId = user.Id, User = user.Email, Id = method, Method = method, Parameters = parameters, UserRoles = userroles })); } else { foreach (var role in validRoles) { if (usrmgr.IsInRole(user.Id, role.Trim())) { return(ApiAuth.Result.Valid(new ApiCall() { IP = c.HostIpAddress, UserId = user.Id, User = user.Email, Id = method, Method = method, Parameters = parameters, UserRoles = userroles })); } } return(ApiAuth.Result.Invalid(new ApiCall() { IP = c.HostIpAddress, UserId = user.Id, User = user.Email, Id = method, Method = method, Parameters = parameters, UserRoles = userroles })); } } } else if (c.User?.Identity?.IsAuthenticated == true) { Microsoft.AspNet.Identity.EntityFramework.IdentityUser user = usrmgr.FindByEmail(c.User.Identity.Name); string[] userroles = usrmgr.GetRoles(user.Id).ToArray(); if (validRoles == null) { return(ApiAuth.Result.Valid(new ApiCall() { IP = c.HostIpAddress, UserId = user?.Id, User = c.User.Identity.Name, Id = method, Method = method, Parameters = parameters, UserRoles = userroles })); } else if (validRoles.Count() == 0) { return(ApiAuth.Result.Valid(new ApiCall() { IP = c.HostIpAddress, UserId = user?.Id, User = c.User.Identity.Name, Id = method, Method = method, Parameters = parameters, UserRoles = userroles })); } else { foreach (var role in validRoles) { if (c.User.IsInRole(role.Trim())) { return(ApiAuth.Result.Valid(new ApiCall() { IP = c.HostIpAddress, UserId = user?.Id, User = c.User.Identity.Name, Id = method, Method = method, Parameters = parameters, UserRoles = userroles })); } } return(ApiAuth.Result.Invalid(new ApiCall() { IP = c.HostIpAddress, UserId = user?.Id, User = c.User.Identity.Name, Id = method, Method = method, Parameters = parameters, UserRoles = userroles })); } } else { return(ApiAuth.Result.Invalid(new ApiCall() { IP = c.HostIpAddress, UserId = null, User = null, Id = method, Method = method, Parameters = parameters })); } }