.net 自托管WCF Rest服务的身份验证
我正在创建一个.Net(C#)应用程序,它有一个web界面,可以从用户的本地网络访问该界面。当然,用户也可以选择从外部(internet)访问web界面 web界面是一个SPA(单页应用程序)。网站有一个Rest界面和一个WCF界面,因此其他软件可以轻松地与我的应用程序交互。两者都是自托管的 该网站本身(HTML页面)是使用自托管的 WCF和Rest的运营合同如下所示:.net 自托管WCF Rest服务的身份验证,.net,wcf,rest,authentication,nancy,.net,Wcf,Rest,Authentication,Nancy,我正在创建一个.Net(C#)应用程序,它有一个web界面,可以从用户的本地网络访问该界面。当然,用户也可以选择从外部(internet)访问web界面 web界面是一个SPA(单页应用程序)。网站有一个Rest界面和一个WCF界面,因此其他软件可以轻松地与我的应用程序交互。两者都是自托管的 该网站本身(HTML页面)是使用自托管的 WCF和Rest的运营合同如下所示: [OperationContract] [WebGet(BodyStyle = WebMessageBodyS
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
public string GetStuff()
{
return "Stuff";
}
// Create the ServiceHost.
apiHost = new ServiceHost(typeof(ServiceClass), Address);
apiHost.Description.Behaviors.Add(smb);
ServiceEndpoint restEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(ServiceClass)), new WebHttpBinding(), new EndpointAddress(RestAddress));
restEndpoint.EndpointBehaviors.Add(new WebHttpBehavior());
apiHost.AddServiceEndpoint(restEndpoint);
WCF/Rest服务的启动方式如下:
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
public string GetStuff()
{
return "Stuff";
}
// Create the ServiceHost.
apiHost = new ServiceHost(typeof(ServiceClass), Address);
apiHost.Description.Behaviors.Add(smb);
ServiceEndpoint restEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(ServiceClass)), new WebHttpBinding(), new EndpointAddress(RestAddress));
restEndpoint.EndpointBehaviors.Add(new WebHttpBehavior());
apiHost.AddServiceEndpoint(restEndpoint);
现在我想添加一个登录到网站和Rest服务(稍后将添加登录到WCF服务)。但每当我尝试在互联网上解释的方法时,有些东西就不起作用了。例如,我尝试了,但无法访问HttpContext.Current.User.Identity,因为当前HttpContext为空
有人能告诉我如何将身份验证添加到我的实现中,或者至少为我指出正确的方向吗?
ServiceContract中有很多方法,因此我不希望在每个方法中都进行一些手动检查。但如果没有其他方法,我可能会与PostSharp合作,为每个方法添加一些身份验证代码
谢谢你的帮助 您需要在HttpContext.Current的web.config中启用aspNetCompatibilityEnabled=“true”以返回某些内容。还需要在服务上设置RequirementsMode=AspNetCompatibilityRequirementsMode.Required。