Ryan Ryan - 1 month ago 14
C# Question

Log into DotNetNuke site remotely

I am creating a simple web service, for DotNetNuke 7, that will allow an iOS or Android app to authenticate a user with our website to make sure they are a member and so allowed to view restricted content on the app.

I have a test webservice running that responds to annonymous connections. How do I actually get it to login into the website and retrieve data back (like roles and contact information)?

public class SiteLoginController : DnnApiController
{
[DnnAuthorize]
[HttpGet]
public HttpResponseMessage LoginToSite(string username, string password)
{
// go log into website
// if they have an account
// retrieve role and contact info
// else
// return invalid credentials message
}
}


I have looked online for a while now and most answers seem to relate to allowing annonymous access and not actually authenticating the user.

Answer

You don't need to log in to get the user info. Although I'm not sure the logging in part works from a web service.

UserInfo user = UserController.GetUserByName(username);

if (user != null)
{
    string email = user.Email;
}
else
{
    //user not found
}

Or if you do want to log in for added security, you can do this:

string resultText = string.Empty;

UserLoginStatus loginStatus = new UserLoginStatus();
UserController.UserLogin(PortalId, username, password, null, PortalSettings.PortalName, DotNetNuke.Services.Authentication.AuthenticationLoginBase.GetIPAddress(), ref loginStatus, false);

switch (loginStatus)
{
    case UserLoginStatus.LOGIN_SUCCESS:
        resultText = "OK";
        break;
    case UserLoginStatus.LOGIN_FAILURE:
        resultText = "Failure";
        break;
    case UserLoginStatus.LOGIN_USERLOCKEDOUT:
        resultText = "Locked out";
        break;
    case UserLoginStatus.LOGIN_USERNOTAPPROVED:
        resultText = "Not approved";
        break;
    default:
        resultText = "Unknown error";
        break;
}
Comments