Agalya T

Agalya T

  • NA
  • 1
  • 652

Error in Invalidcast exception type

Jun 16 2015 5:18 AM
While logging the web page the global.asax.cs triggers the Application_AuthenticateRequest method and triggers the event handler Application_AuthenticateRequest method in securitymodule.cs .
But in my code it was not triggering the Application_AuthenticateRequest method in securitymodule.cs.Due to this getting exception like system.invalidcastexception:"Unable to cast object of type 'System.Web.Security.FormsIdentity' to type 'AllianzLife.WebServices.Security.AllianzIdentity'" in the line
 return ((AllianzIdentity)HttpContext.Current.User.Identity).Username;  in securitycontent.cs file.
 
Kindly can anyone help me on the same.
 
Global.asax.cs
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
 
SecurityModule.cs
public void Init(HttpApplication application)
{
this.application = application;
application.AuthenticateRequest +=
new EventHandler(Application_AuthenticateRequest); //Occurs when a security module has established the identity of the user
application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);
application.BeginRequest +=
new EventHandler(application_BeginRequest);
}
 
private void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated &&HttpContext.Current.User.Identity is FormsIdentity)
{
 
FormsIdentity formsIdentity = (FormsIdentity) HttpContext.Current.User.Identity;
principal =
new AllianzPrincipal(formsIdentity.Ticket);
 
HttpContext.Current.User = principal;
SecurityUtil.ResetAuthenticationTicket(principal);
}
}
}
 
Securitycontent.cs

public string Username

{

get

{

if (!HasAuthenticated)

{

return null;

}

return ((AllianzIdentity)HttpContext.Current.User.Identity).Username;

}

}