1
처리를 시작하기 전에 들어오는 요청을 인증해야합니다. 내 클라이언트 응용 프로그램이 서버에 요청할 때 기본 인증을 사용하여 요청을 인증해야하며 클라이언트에 응답을 보내야합니다.asp.mvc에서 요청을 처리하기 전에 기본 인증을 사용하는 방법은 무엇입니까? (내부 설명)
나는
public class OptionalAuthentication : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContextWrapper httpContext = new HttpContextWrapper(System.Web.HttpContext.Current);
HttpRequestBase httpRequest = httpContext.Request;
if ((httpContext.User == null ? true : !httpContext.User.Identity.IsAuthenticated))
{
var request = HttpContext.Current.Request;
//request.Headers.FirstOrDefault(h => h.Key.Equals("Authorization"));
var authHeader = request.Headers["Authorization"];
if (authHeader != null)
{
var authHeaderVal = AuthenticationHeaderValue.Parse(authHeader);
// RFC 2617 sec 1.2, "scheme" name is case-insensitive
if (authHeaderVal.Scheme.Equals("basic",
StringComparison.OrdinalIgnoreCase) &&
authHeaderVal.Parameter != null)
{
if (AuthenticateUser(authHeaderVal.Parameter))
{
}
}
}
}
else
{
//log.Trace("user is already authenticated: '{0}'", httpContext.User.Identity.Name);
}
}
}
[OptionalAuthentication]
public ActionResult Index(string projectSlug, string repositoryName)
{
ActionResult emptyResult;
if (Request.IsAuthenticated)
{
var cred = System.Text.ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(Request.Headers["Authorization"].Substring(6))).Split(':');
}
}
누군가가 나에게 이것을 달성 할 수있는 방법을 알 수 있습니다, 다음을 시도했지만 실패했습니다. 여기