asp.net web api文档

时间:2016-03-29 11:41:36

标签: asp.net asp.net-web-api

我开发了一个web api项目和一个管理面板(web项目)。

我只希望管理员用户可以访问web api文档。

web api和admin项目作为IIS中的虚拟目录托管。

有没有办法实现这个目标?

我已经在管理项目的gloabl.asax中编写了代码集主体,下面是代码:

try
{
    //let us take out the username now                
    string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
    string roles = "Admin";

    //Let us set the Pricipal with our user specific details
    HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(
        new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';'));
}

请协助。

1 个答案:

答案 0 :(得分:0)

您需要首先在web api项目中启用cookie身份验证。您可以在2个应用程序之间共享cookie。确保每个应用程序中的Cookie名称相同。 请参阅http://www.hanselman.com/blog/SharingAuthorizationCookiesBetweenASPNET4xAndASPNETCore10.aspx