自定义授权属性
我正在构建自己的会员系统,我不想与 MS 会员提供商有任何关系。我浏览过互联网和 StackOverflow,但我所能找到的只是建立在 MS 会员资格提供商之上的会员资格提供商。
不管怎样,我现在几乎已经把所有东西都连接好了,但我想使用一个利用我的会员基础设施的自定义授权属性。我在网站上查看了这个线程,我正在尝试做类似的事情,但我不确定这是否是我需要的安静。到目前为止,这些是我已经得到的类:
SessionManager:
public static class SessionManager : ISessionManager
{
public static void RegisterSession(string key, object obj)
{
System.Web.HttpContext.Current.Session[key] = obj;
}
public static void FreeSession(string key)
{
System.Web.HttpContext.Current.Session[key] = null;
}
public static bool CheckSession(string key)
{
if (System.Web.HttpContext.Current.Session[key] != null)
return true;
else
return false;
}
public static object ReturnSessionObject(string key)
{
if (CheckSession(key))
return System.Web.HttpContext.Current.Session[key];
else
return null;
}
}
SharweAuthorizeAttribute: (我不太确定这是否真的是我应该做的 )
public class SharweAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (SessionManager.CheckSession(SessionKeys.User) == true)
return true;
else
return false;
}
}
现在这就是我需要的:
- 是我的 SharweAuthorizeAttribute 类 首先正确吗?
- 我需要能够重定向 未经身份验证的用户登录 页面
我需要根据以下内容授权用户 他们的角色(使用我自己的角色 提供商)所以我会做一些事情 像:
[SharweAuthorize(Roles="MyRole")]
我想就是这样......任何建议都非常受欢迎:)
更新: 好吧,我刚刚再次阅读该页面,找到了第二个问题的解决方案:
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (SessionManager.CheckSession(SessionKeys.User) == false)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "action", "ActionName" },
{ "controller", "ControllerName" }
});
}
else
base.HandleUnauthorizedRequest(filterContext);
}
如果我做对了,请告诉我......
I'm building my own membership system and I want nothing to do with the MS Membership provider. I've looked around the internet and here on StackOverflow but all I could found was membership providers built on top of the MS Membership provider.
Anyway, I've got almost everything hooked up now, but I'd like to use a custom Authorize attribute which utilized my membership infrastructure. I checked out this thread here on the site and I'm trying to do something similar, but I'm not sure that's quiet what I need. So far these are the classes I've got:
SessionManager:
public static class SessionManager : ISessionManager
{
public static void RegisterSession(string key, object obj)
{
System.Web.HttpContext.Current.Session[key] = obj;
}
public static void FreeSession(string key)
{
System.Web.HttpContext.Current.Session[key] = null;
}
public static bool CheckSession(string key)
{
if (System.Web.HttpContext.Current.Session[key] != null)
return true;
else
return false;
}
public static object ReturnSessionObject(string key)
{
if (CheckSession(key))
return System.Web.HttpContext.Current.Session[key];
else
return null;
}
}
SharweAuthorizeAttribute: (I am not really sure if that's actually what I should be doing)
public class SharweAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (SessionManager.CheckSession(SessionKeys.User) == true)
return true;
else
return false;
}
}
Now here's what I need:
- Is my SharweAuthorizeAttribute class
correct in the first place? - I need to be able to redirect
unauthenticated users to the login
page I need to authorize users based on
their roles (using my own role
provider) so I would do something
like:[SharweAuthorize(Roles="MyRole")]
That's it I guess... Any suggestions are more than welcome :)
UPDATE:
Ok I just read that page again and found the solution to question number two:
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (SessionManager.CheckSession(SessionKeys.User) == false)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "action", "ActionName" },
{ "controller", "ControllerName" }
});
}
else
base.HandleUnauthorizedRequest(filterContext);
}
Let me know if I got it right please...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您做对了(IMO 实现自定义会员资格提供程序更安全、更简单,但这是您的选择)
AuthorizeAttribute 继承了
基类,然后检查用户是否处于该角色中的实现。roles
属性编辑:更多关于角色的信息,
如果你有的话
,你可以检查 AuthorizeCore 方法中的 Roles 属性
Yes, you got it right (IMO it's safer and simpler to implement a custom membership provider, but it's your choice)
roles
property from theAuthorizeAttribute
base class and you check in your implementation if the user is in the role.Edit: a little more on the roles thing
if you have
then you can check the Roles property in the AuthorizeCore method