Reputation: 3082
I have created an MVC application whereby the user is able to authenticate either via ADFS or Forms login and in both cases I have been able to use the User.IsInRole method to check my user table that has a roleID attribute which ties to a role table in my database. This is done by including the following section in my webconfig within :
<roleManager enabled="true" defaultProvider="DefaultRoleProvider" cacheRolesInCookie="true">
<providers>
<clear />
<add name="DefaultRoleProvider" type="MyInternal.Providers.MyProvider, MyInternal" connectionStringName="MyContext" />
</providers>
</roleManager>
I am now trying to implement windows authentication and have managed to get the users domain login name etc but when trying to follow same steps as with other two authentication types I am unable to get the IsInRole to work.
How can I tie the User from my repository to the authentication user. Is there some sort of casts or something required? I think that this may have been done by the authentication in ADFS and Forms.
Upvotes: 1
Views: 774
Reputation: 3082
I was able to solve this issue by using the following within my ViewModel:
this.UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
if (this.UserName.Contains("\\"))
{
string[] stringArray = this.UserName.Split(new Char[] { '\\' });
this.UserName = stringArray[1];
MyUser identity = userRepository.Get(u => u.Username == this.UserName).FirstOrDefault();
HttpContext.Current.User = identity;
}
Upvotes: 1