Handling Login.Authenticate event

北慕城南 提交于 2019-12-20 04:32:37

问题



A) Book I’m learning from says that if we handle Login.Authenticate event, then we have to authenticate users on our own. Thus control won’t automatically validate username and password. I thought book suggested this would only happen if we override Login.OnAuthenticate() method, but it appears that even if only add an event handler for Authenticate event, the automatic authentication doesn’t happen.

But why is that? Why doesn’t event handling work like it does with Init or Load events, where you essentially have to override Page.OnInit() and Page.OnLoad() in order gain control over event handling?


B) I checked MSDN site and it basically recommends that if we do override Login.OnAuthenticate(), we should also call base.OnAuthenticate(). But then, why would we ever need to override Login.OnAuthenticate(), if we get the same effect with simply declaring an event handler for Login.Authenticate?


thanx


回答1:


You should not override OnAuthenticate. This method is only used internally to either raise the Authenticate event to registered handlers if there are any or authenticate via the current MembershipProvider if there are no handlers registered. So, to implement custom authentication for the Login control, you simply register a handler for the Authenticate event and set the AuthenticateEventArgs.Authenticated property.

Event handling for the Authenticate event works exactly the same as for other events. The only difference is that the OnAuthenticate method has some logic that determines whether to use a MembershipProvider or a registered event handler for authentication.

If you do create a subclass of Login and override Login.OnAuthenticate, it is wise to call base.OnAuthenticate(...) because it contains the logic that calls the registered event handlers. If you do not call base.Authenticate(...), you should call the registered event handlers yourself. But creating a subclass of Login is probably not necessary in your situation.



来源:https://stackoverflow.com/questions/821395/handling-login-authenticate-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!