问题描述
首先我自定义了一个用户身份验证,在这里我通过Authorization的认证判断用户是否存在publicclassBasicAuthorizeAttribute:System.Web.Http.AuthorizeAttribute{publicoverridevoidOnAuthorization(System.Web.Http.Controllers.HttpActionContextactionContext){//读取HEADER信息验证if(actionContext.Request.Headers.Authorization!=null){stringTicketInfo=actionContext.Request.Headers.Authorization.ToString();//header里的Authorization:ticketif(ValidateUserTicket(TicketInfo)){IsAuthorized(actionContext);//Users="12";}else{HandleUnauthorizedRequest(actionContext);}}else{HandleUnauthorizedRequest(actionContext);}}protectedoverridevoidHandleUnauthorizedRequest(System.Web.Http.Controllers.HttpActionContextactionContext){//varchallengeMessage=newSystem.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);//challengeMessage.Headers.Add("WWW-Authenticate","Basic");//thrownewSystem.Web.Http.HttpResponseException(challengeMessage);base.HandleUnauthorizedRequest(actionContext);if(actionContext.Response.StatusCode==System.Net.HttpStatusCode.Unauthorized){//401错误//actionContext.Response.Result=newRedirectResult("/");}}
在写API接口的时候,因为已经加了BasicAuthorize认证了,那么在GET方法的时候怎么获取当前已经验证的用户信息呢?[Auth.BasicAuthorize]publicclassValuesController:ApiController{//GETapi/valpublicIEnumerable<string>Get(){returnnewstring[]{"value1","value2"};//在这里如何将在BasicAuthorize用户认证的信息传递过来}
解决方案
解决方案二:
在线等大神!
解决方案三:
自己再顶一下
解决方案四:
拜托各位大神指点一下,小弟只有这么点分了,谢谢!