我正在使用Servicestack作为我的asp.net mvc应用程序的中间层逻辑/ Web服务部分。在前端,我使用FormsAuthentication +“auth / credentials”来启用对asp.net和servicestack的身份验证/授权。
在我的一些mvc控制器中,我使用内置的User (IPrincipal)
进行角色/权限检查,
[Authorize]
public ActionResult Index()
{
if (!User.IsInRole("admin"))
ViewBag.ErrorMessage = "Access Denied";
return View();
}
我了解到默认User
中的角色在表单身份验证期间没有填充,因此我创建了基于IPrincipal的自定义类,并在调用{{{}期间将其设置为HttpContext.Current.User
1}}
这是正确的方法来获取/设置从servicestack到PostAuthenticateRequest
的角色/权限吗?
HttpContext.Current.User
顺便说一句,这是我的Login方法,请检查这是否也是双重身份验证到asp.net formsauthentication和servicestack的正确方法。 :)
public class CustomPrincipal : IPrincipal
{
//private readonly IEnumerable<string> _roles;
public IIdentity Identity { get; private set; }
public bool IsInRole(string role)
{
// retrieve roles from servicestack cache
var key = ServiceStack.ServiceInterface.SessionFeature.GetSessionKey() ?? "";
var sess = fubar.web.App_Start.AppHost.Resolve<ServiceStack.CacheAccess.ICacheClient>().Get<ServiceStack.ServiceInterface.Auth.AuthUserSession>(key);
if (sess != null)
return sess.Roles.Contains(role);
return false;
}
public CustomPrincipal(string name)
{
Identity = new GenericIdentity(name);
//_roles = roles;
}
public long Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
}
非常感谢!