如何编写自己的AuthorizeTag?
我放弃了 asp.net 会员资格,所以我猜我需要自己的授权标签(如果我错了,请纠正我)。因为可能他们所拥有的都指向会员级别(但不知道如何验证这一点)。
现在我尝试做这个
公共类 MyTest :AuthorizeAttribute {
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null) throw new ArgumentNullException("httpContext");
// Make sure the user is authenticated.
if (httpContext.User.Identity.IsAuthenticated == false)
{
return false;
}
else
{
return false;
}
}
}
然后,在我的默认视图中,我有这个。
FormsAuthentication.SetAuthCookie("xiao", true);
然后我有另一个观点
[MyTest()] 公共 ActionResult 关于() { 返回视图(); 然后
我转到这个视图,我仍然可以访问它。我看到它把这个放在 url 中
"LogOn?ReturnUrl=%2fHome%2fAbout"
,但最重要的是我仍然可以看到该页面(和所有内容)。当我至少应该看到你未经授权或类似的东西时。
我做错了什么?
谢谢
,好吧,它现在似乎可以工作,但我仍然不知道如何传递角色。
I ditching asp.net membership so I am guessing I need my own authorize tags(correct me if I am wrong). Since probably the ones they have all point to the membership classes(Not sure how to verify this though).
Now I tried to do this
public class MyTest : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null) throw new ArgumentNullException("httpContext");
// Make sure the user is authenticated.
if (httpContext.User.Identity.IsAuthenticated == false)
{
return false;
}
else
{
return false;
}
}
}
I then in my defautl view I have this.
FormsAuthentication.SetAuthCookie("xiao", true);
I then have on another view
[MyTest()]
public ActionResult About()
{
return View();
}
I then go to this view and I am still able to access it. I see that it puts this in the url
"LogOn?ReturnUrl=%2fHome%2fAbout"
but bottom line it is I still can see the page(and all the content). When I should see just at the very least your not authorized or something like that.
what am I doing wrong?
Thanks
Ok it seems to work now but I still don't know how to pass in roles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以实现自定义会员资格提供程序,这样可以避免很多麻烦。
You can implement a custom membership provider and save yourself a lot of grief.
自定义角色提供程序也会有所帮助。 。
A Custom Role Provider will also be helpful...