如何保存和访问UserContext.Identity(MVC/C#)以与AngularJS一起使用?
在我的MVC应用程序C#中,我有一个带有下拉菜单的页面部分。当进行选择时,MVC控制器将如何保存和访问UserContext.Identity(MVC/C#)以与AngularJS一起使用?,c#,angularjs,asp.net-mvc,C#,Angularjs,Asp.net Mvc,在我的MVC应用程序C#中,我有一个带有下拉菜单的页面部分。当进行选择时,MVC控制器将UserContext.Identity.CurrentProject设置为对象 这对于在服务器端进行访问非常好,但是如何使用Angular进行设置,以便从任何地方都可以访问对象 谢谢 --更新-- 我已经在一个基础设施项目中创建了自己的UserContext类: using System; using System.Collections.Generic; using System.Linq; using
UserContext.Identity.CurrentProject
设置为对象
这对于在服务器端进行访问非常好,但是如何使用Angular进行设置,以便从任何地方都可以访问对象
谢谢
--更新--
我已经在一个基础设施
项目中创建了自己的UserContext
类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AdminWebsite.Infrastructure
{
public static class UserContext
{
public static CustomPrincipal User
{
get
{
return (CustomPrincipal)HttpContext.Current.User;
}
}
public static CustomIdentity Identity
{
get
{
return (CustomIdentity)HttpContext.Current.User.Identity;
}
}
}
}
。。。在这里,我创建了自己的自定义成员资格/主体/配置文件/角色提供者
从Razor的角度来看,当我这样做时:
<script language="javascript">
alert("hello: " + @UserContext.Identity.CurrentProject.WebsiteName);
</script>
在主页上的某个地方,您将需要设置如下内容:
var currentProject = '@UserContext.Identity.CurrentProject';
// Not sure what version of MVC you're using
然后在你的应用程序中,你可以让它成为一个常数,也许:
angular.module('myApp')
.constant('myConstants', {
projectName : currenctProject
});
现在,您可以将它注入到任何需要的地方(作为依赖项),并通过myConstants.projectName
访问它
但这是一种非常有棱角的方式。您也可能是懒惰的,只需访问
currentProject
) 剃刀代码在哪里?您是否已确认该用户已通过身份验证?您在哪里设置当前用户的身份(在global.asax?)Razor代码位于顶级Layout.cshtml
视图中。上面更新了Global.asax代码所在的位置。在调用razor代码之前,您是否已验证用户正在获得身份验证/您发布的全局代码正在执行?
angular.module('myApp')
.constant('myConstants', {
projectName : currenctProject
});