加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – 使用Windows身份验证获取WCF中当前正在交互的

发布时间:2020-12-16 09:37:00 所属栏目:asp.Net 来源:网络整理
导读:在我正在开发的MVC3项目中,我们试图将当前在控制器中的许多逻辑移动到服务层中,并将其作为WCF中的REST服务公开. 所以在我们的Global.asax中我们创建一个服务路由,如下所示: RouteTable.Routes.Add(new ServiceRoute ("Exampleservice",new WebServiceHostFa
在我正在开发的MVC3项目中,我们试图将当前在控制器中的许多逻辑移动到服务层中,并将其作为WCF中的REST服务公开.

所以在我们的Global.asax中我们创建一个服务路由,如下所示:

RouteTable.Routes.Add(new ServiceRoute
    ("Exampleservice",new WebServiceHostFactory(),typeof(ExampleService)));

我们的控制器访问这样的服务:

public class ExampleController : Controller {
    private IExampleService service;

    public ExampleController() {
        this.service = new ExampleService();
    } 

    public ActionResult Index() {
         var results = service.GetAll();
         return View(results);
    }
}

这里的要点是我们直接使用服务类(不通过HttpClient通过网络发出请求).

我们的网站使用Windows身份验证(它是一个内部网站点),我们希望保持这种方式.我的问题是,有没有办法可以在服务类中获得用户身份,这对于我们如何使用服务的控制器以及WCF使用服务的方式都有效?

例如:

[ServiceContract]
public interface IExampleService
{
    [WebGet(UriTemplate = "/")]
    List<Results> GetAll();
}

public class ExampleService : IExampleService
{
     List<Results> GetAll() {
         // Get User Name Here
         // In ASP.Net I would use User.Identity.Name
         // If I was just worrying about the the REST service I would use
         // ServiceSecurityContext.Current.WindowsIdentity.Name
     }
}

解决方法

@ Ryand.Johnson建议的指示是正确的.这里的要点是控制器不向Web服务发送任何凭据,因为它在asp.net用户身份下运行,而不是当前登录用户的身份.将标识传递给代理的唯一方法是通过以下方式在模拟上下文中嵌入对Web服务的调用:

using (WindowsImpersonationContext impersonatedUser = (User.Identity as System.Security.Principal.WindowsIdentity).Impersonate()){ 
     //your proxy call here  }

如果仍然以这种方式获得null,则必须手动将默认凭据设置为代理

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读